﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using DELVE_GAME.ViewStructure.GameActions;
using DELVE_GAME.ViewStructure.Tutorial;
using System.Data;
using System.IO;
using DELVE_GAME.ViewStructure.MapEditor;

namespace DELVE_GAME.ViewStructure
{
    enum STAGES
    {
        MAIN_MENU = 0,
        GAME_CONNECTION = 1,
        GAME_LOBBY = 2,
        MAP_CREATION_MENU = 3,
        MAP_CREATOR = 4,
        MAP_SELECTION = 5,
        GAMETYPE = 6,
        GAME_OPTIONS = 7,
        PROFILE_SELECTION = 8,
        PROFILE_CREATION = 9,
        ENTER_IP_ADDRESS = 10,
        GAME_OVER = 12,
        TUTORIAL_START = 13,
        LEADERBOARD_START = 110,
        END_GAME = 200,

        REPLAY_MENU = 1000,
    }

    class View : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private Game game;
        private GameSettings gameSettings;
        private Board board;
        private SpriteBatch spriteBatch;
        Dictionary<String, Texture2D> textures;
        Dictionary<String, SpriteFont> fonts;
        List<Stage> stages;
        int playStageIndex;
        List<Keys> oldKeysPressed;
        TextComponent gameOverMessage;
        int profileMenuOptionIndex = 0;
        Rectangle profileChoiceOverlay;
        Rectangle colorChoiceOverlay;
        private bool isNetworked;
        MenuOption profileName;

        int currentStage = 0;
        int x;
        int y;

        private bool isMouseLeftReleased;
        static String hudString = "";
        static int hudTileType = 0;
        String playerScoresHud = "";
        TextComponent playerScores;
        ReplayManager replayManager;
        MapManager mapManager;
        SoundManager soundManager;
        TextEditor textEditor;
        SoundEffectInstance sound;
        PlayerProfileManager profileManager;
        List<MenuChoice2> playerProfileChoices;
        List<MenuChoice2> colorChoices;
        List<MenuOption> menuOptions;
        MenuOption ipAddress;
        private bool isHost;

        #region alliance buttons
        /*Button sendRequestButton01;
        Button sendRequestButton02;
        Button sendRequestButton03;
        Button sendRequestButton10;
        Button sendRequestButton12;
        Button sendRequestButton13;
        Button sendRequestButton20;
        Button sendRequestButton21;
        Button sendRequestButton23;
        Button sendRequestButton30;
        Button sendRequestButton31;
        Button sendRequestButton32;

        Button acceptRequestButton01;
        Button acceptRequestButton02;
        Button acceptRequestButton03;
        Button acceptRequestButton10;
        Button acceptRequestButton12;
        Button acceptRequestButton13;
        Button acceptRequestButton20;
        Button acceptRequestButton21;
        Button acceptRequestButton23;
        Button acceptRequestButton30;
        Button acceptRequestButton31;
        Button acceptRequestButton32;

        Button cancelRequestButton01;
        Button cancelRequestButton02;
        Button cancelRequestButton03;
        Button cancelRequestButton10;
        Button cancelRequestButton12;
        Button cancelRequestButton13;
        Button cancelRequestButton20;
        Button cancelRequestButton21;
        Button cancelRequestButton23;
        Button cancelRequestButton30;
        Button cancelRequestButton31;
        Button cancelRequestButton32;

        Button breakAllianceButton01;
        Button breakAllianceButton02;
        Button breakAllianceButton03;
        Button breakAllianceButton10;
        Button breakAllianceButton12;
        Button breakAllianceButton13;
        Button breakAllianceButton20;
        Button breakAllianceButton21;
        Button breakAllianceButton23;
        Button breakAllianceButton30;
        Button breakAllianceButton31;
        Button breakAllianceButton32;

        bool requestSent01 = false;
        bool requestSent02 = false;
        bool requestSent03 = false;
        bool requestSent10 = false;
        bool requestSent12 = false;
        bool requestSent13 = false;
        bool requestSent20 = false;
        bool requestSent21 = false;
        bool requestSent23 = false;
        bool requestSent30 = false;
        bool requestSent31 = false;
        bool requestSent32 = false;

        bool requestAccepted01 = false;
        bool requestAccepted02 = false;
        bool requestAccepted03 = false;
        bool requestAccepted10 = false;
        bool requestAccepted12 = false;
        bool requestAccepted13 = false;
        bool requestAccepted20 = false;
        bool requestAccepted21 = false;
        bool requestAccepted23 = false;
        bool requestAccepted30 = false;
        bool requestAccepted31 = false;
        bool requestAccepted32 = false;
        /**/
        #endregion

        public View(Microsoft.Xna.Framework.Game g, Game game)
            : base(g)
        {
            textures = new Dictionary<string, Texture2D>();
            fonts = new Dictionary<string, SpriteFont>();
            stages = new List<Stage>();
            //this.game = game;
            isMouseLeftReleased = true;
            oldKeysPressed = new List<Keys>();
            replayManager = new ReplayManager();
            mapManager = new MapManager();
            profileManager = new PlayerProfileManager();
            textEditor = new TextEditor();
            playerProfileChoices = new List<MenuChoice2>();
            colorChoices = new List<MenuChoice2>();
            menuOptions = new List<MenuOption>();
        }

        public override void Initialize()
        {
            base.Initialize();
            spriteBatch = new SpriteBatch(this.GraphicsDevice);
            sound = soundManager.getSound((int)SOUNDS.MENU_BGM);
            sound.IsLooped = true;
            sound.Play();
            isNetworked = false;
            isHost = false;
        }

        public Game getGame() {
            return game;
        }

        protected override void LoadContent()
        {
            Texture2D plainTexture = new Texture2D(GraphicsDevice, 1, 1);
            plainTexture.SetData(new Color[] { Color.White });
            textures.Add("spr_plain", plainTexture);
            soundManager = new SoundManager(this.Game.Content);

            string[] directories = { "Sprites", "TutorialImages" };
            foreach (string directory in directories)
            {
                foreach (string file in Directory.GetFiles("Content/" + directory))
                {
                    string asset = Path.GetFileNameWithoutExtension(file);
                    textures.Add(asset, this.Game.Content.Load<Texture2D>(directory + "/" + asset));
                }
            }
            textures.Add("bg_matrix", this.Game.Content.Load<Texture2D>("bg_matrix"));
            
            fonts.Add("menuFont", this.Game.Content.Load<SpriteFont>("Fonts/MenuFont"));
            fonts.Add("font_potencyBar", this.Game.Content.Load<SpriteFont>("Fonts/font_potencyBar"));
            buildStages();
            base.LoadContent();
        }

        private void buildStages()
        {
            buildMainMenu();
            buildGameConnectionMenu();
            buildGameLobbyMenu();
            //buildReplayMenu();
            buildMapCreationMenu();
            buildMapCreatorStages();
            buildMapSelectionMenu();
            buildGametypeMenu();
            buildOptionsMenu();
            buildProfileMenu();
            buildCreateProfile();
            buildIPAddressMenu();
            buildGameOverStage();
            buildTutorialStages();
            buildLeaderboardMenu();
            
        }

        private void buildGameConnectionMenu()
        {
            MenuStage gameConnectionMenu =
                new MenuStage("Select Game Connection", fonts["menuFont"], GraphicsDevice.Viewport.Width / 2, 0,
                    soundManager.getSound((int)SOUNDS.MENU_CHANGE));

            x = GraphicsDevice.Viewport.Width / 2;
            y = 0;

            //background
            gameConnectionMenu.setBackground(textures["bg_matrix"]);

            MenuOption consoleGameOption = new MenuOption(spriteBatch, x, y + 50, "Console Game", fonts["menuFont"], true);
            MenuOption networkGameOption = new MenuOption(spriteBatch, x, y + 100, "Network Game", fonts["menuFont"], true);
            MenuOption backOption = new MenuOption(spriteBatch, x, y + 150, "Back", fonts["menuFont"], true);

            consoleGameOption.setEnterAction(
                new MenuGameAction(this, (int)STAGES.MAP_SELECTION, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            networkGameOption.setEnterAction(
                new MenuGameAction(this, (int)STAGES.GAME_LOBBY, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            backOption.setEnterAction(
                new MenuGameAction(this, (int)STAGES.MAIN_MENU, soundManager.getSound((int)SOUNDS.MENU_SELECT)));

            gameConnectionMenu.addOption(consoleGameOption);
            gameConnectionMenu.addOption(networkGameOption);
            gameConnectionMenu.addOption(backOption);

            stages.Insert((int)STAGES.GAME_CONNECTION, gameConnectionMenu);

        }

        private void buildGameLobbyMenu()
        {
            MenuStage gameLobbyMenu =
                new MenuStage("Select Join/Create Game", fonts["menuFont"], GraphicsDevice.Viewport.Width / 2, 0,
                    soundManager.getSound((int)SOUNDS.MENU_CHANGE));

            x = GraphicsDevice.Viewport.Width / 2;
            y = 0;

            //background
            gameLobbyMenu.setBackground(textures["bg_matrix"]);

            MenuOption createGameOption = new MenuOption(spriteBatch, x, y + 50, "Create Game", fonts["menuFont"], true);
            MenuOption joinGameOption = new MenuOption(spriteBatch, x, y + 100, "Join Game", fonts["menuFont"], true);
            MenuOption backOption = new MenuOption(spriteBatch, x, y + 150, "Back", fonts["menuFont"], true);

            createGameOption.setEnterAction(
                new MenuGameAction(this, (int)STAGES.MAP_SELECTION, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            joinGameOption.setEnterAction(
                new MenuGameAction(this, (int)STAGES.ENTER_IP_ADDRESS, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            backOption.setEnterAction(
                new MenuGameAction(this, (int)STAGES.GAME_CONNECTION, soundManager.getSound((int)SOUNDS.MENU_SELECT)));

            gameLobbyMenu.addOption(createGameOption);
            gameLobbyMenu.addOption(joinGameOption);
            gameLobbyMenu.addOption(backOption);

            stages.Insert((int)STAGES.GAME_LOBBY, gameLobbyMenu);
        }
        
        private void buildMapCreatorStages()
        {
            int offset = 0;

            Map newMap = mapManager.getMap();

            MapEditorInitStage initMapStage = new MapEditorInitStage(newMap,
                                                             fonts["menuFont"],
                                                             new Sprite(textures["spr_upArrow"], 13, 13, 4, 4),
                                                             new Sprite(textures["spr_downArrow"], 13, 13, 4, 4)
                                                           );
            if ((int)STAGES.MAP_CREATOR + offset >= stages.Count)
                stages.Add(initMapStage);
            else
                stages.Insert((int)STAGES.MAP_CREATOR + offset, initMapStage);
            offset++;

            MapDrawer md = new MapDrawer(newMap, new Sprite(textures["spr_tiles"], 32, 32, 9, 18));



            MapEditorStage mapEditorStage = new MapEditorStage(this, new Sprite(textures["spr_tiles"], 32, 32, 9, 18), textures["drag_rectangle"], newMap, md);
            if ((int)STAGES.MAP_CREATOR + offset >= stages.Count)
                stages.Add(mapEditorStage);
            else
                stages.Insert((int)STAGES.MAP_CREATOR + offset, mapEditorStage);
            offset++;


            GameAction startMapEditingButton = 
                new MapCreatorGameAction(this, initMapStage, mapEditorStage, soundManager.getSound((int)SOUNDS.MENU_SELECT));
            Button beginButton = new Button(400, 300, new Sprite(textures["spr_beginEditingButton"], 96, 32, 1, 3));
            initMapStage.add(beginButton);
            beginButton.setMouseReleasedAction(startMapEditingButton);

            GameAction regularTileAction = 
                new SetTileTypeGameAction(mapEditorStage, md, newMap, TILE_VARIANTS.TILE, 
                    soundManager.getSound((int)SOUNDS.TILE_CLICK));
            Button regularTileButton = new Button(700, 50, new Sprite(textures["spr_regularTileButton"], 64, 64, 3, 3));
            mapEditorStage.add(regularTileButton);
            regularTileButton.setMouseReleasedAction(regularTileAction);

            GameAction highBandwidthAction = new SetTileTypeGameAction(mapEditorStage, md, newMap, TILE_VARIANTS.HIGH_BANDWIDTH,
                    soundManager.getSound((int)SOUNDS.TILE_CLICK));
            Button highBandwidthButton = new Button(700, 125, new Sprite(textures["spr_highBandwidthButton"], 64, 64, 3, 3));
            mapEditorStage.add(highBandwidthButton);
            highBandwidthButton.setMouseReleasedAction(highBandwidthAction);

            GameAction lowBandwidthAction = new SetTileTypeGameAction(mapEditorStage, md, newMap, TILE_VARIANTS.LOW_BANDWIDTH,
                    soundManager.getSound((int)SOUNDS.TILE_CLICK));
            Button lowBandwidthButton = new Button(700, 200, new Sprite(textures["spr_lowBandwidthButton"], 64, 64, 3, 3));
            mapEditorStage.add(lowBandwidthButton);
            lowBandwidthButton.setMouseReleasedAction(lowBandwidthAction);

            GameAction wifiAction = new SetTileTypeGameAction(mapEditorStage, md, newMap, TILE_VARIANTS.WIFI,
                    soundManager.getSound((int)SOUNDS.TILE_CLICK));
            Button wifiButton = new Button(775, 50, new Sprite(textures["spr_wifiButton"], 64, 64, 3, 3));
            mapEditorStage.add(wifiButton);
            wifiButton.setMouseReleasedAction(wifiAction);

            GameAction firewallAction = new SetTileTypeGameAction(mapEditorStage, md, newMap, TILE_VARIANTS.FIREWALL,
                    soundManager.getSound((int)SOUNDS.TILE_CLICK));
            Button firewallButton = new Button(775, 125, new Sprite(textures["spr_firewallButton"], 64, 64, 3, 3));
            mapEditorStage.add(firewallButton);
            firewallButton.setMouseReleasedAction(firewallAction);

            GameAction busAction = new SetTileTypeGameAction(mapEditorStage, md, newMap, TILE_VARIANTS.BUS,
                    soundManager.getSound((int)SOUNDS.TILE_CLICK));
            Button busButton = new Button(775, 200, new Sprite(textures["spr_busButton"], 64, 64, 3, 3));
            mapEditorStage.add(busButton);
            busButton.setMouseReleasedAction(busAction);

            GameAction brokenLinkAction = new SetTileTypeGameAction(mapEditorStage, md, newMap, TILE_VARIANTS.BROKEN_LINK,
                    soundManager.getSound((int)SOUNDS.TILE_CLICK));
            Button brokenLinkButton = new Button(850, 50, new Sprite(textures["spr_brokenLinkButton"], 64, 64, 3, 3));
            mapEditorStage.add(brokenLinkButton);
            brokenLinkButton.setMouseReleasedAction(brokenLinkAction);

            GameAction dyingLinkAction = new SetTileTypeGameAction(mapEditorStage, md, newMap, TILE_VARIANTS.DYING_LINK,
                    soundManager.getSound((int)SOUNDS.TILE_CLICK));
            Button dyingLinkButton = new Button(850, 125, new Sprite(textures["spr_dyingLinkButton"], 64, 64, 3, 3));
            mapEditorStage.add(dyingLinkButton);
            dyingLinkButton.setMouseReleasedAction(dyingLinkAction);



            GameAction setStartingAreasAction = new SetStartingAreasGameAction(mapEditorStage, md, newMap,
                    soundManager.getSound((int)SOUNDS.BASE_UPGRADE));
            Button startingAreasButton = new Button(700, 400, new Sprite(textures["spr_startingTerritoryButton"], 96, 32, 1, 3));
            mapEditorStage.add(startingAreasButton);
            startingAreasButton.setMouseReleasedAction(setStartingAreasAction);


            GameAction setTerritoriesAction = new SetTerritoriesGameAction(mapEditorStage, md, newMap,
                    soundManager.getSound((int)SOUNDS.BASE_UPGRADE));
            Button territoriesButton = new Button(700, 433, new Sprite(textures["spr_territoriesSetButton"], 96, 32, 1, 3));
            mapEditorStage.add(territoriesButton);
            territoriesButton.setMouseReleasedAction(setTerritoriesAction);


            GameAction setRaceTerritoryAction = new SetRaceTerritoryGameAction(mapEditorStage, md, newMap,
                    soundManager.getSound((int)SOUNDS.BASE_UPGRADE));
            Button raceTerritoryButton = new Button(700, 466, new Sprite(textures["spr_raceTerritoryButton"], 96, 32, 1, 3));
            mapEditorStage.add(raceTerritoryButton);
            raceTerritoryButton.setMouseReleasedAction(setRaceTerritoryAction);

    /////////viewing buttons

            GameAction viewTerritoriesAction = new ViewTerritoriesGameAction(mapEditorStage, md, newMap,
                    soundManager.getSound((int)SOUNDS.BASE_UPGRADE));
            Button viewTerritoriesButton = new Button(797, 433, new Sprite(textures["spr_viewterritoriesSetButton"], 96, 32, 1, 3));
            mapEditorStage.add(viewTerritoriesButton);
            viewTerritoriesButton.setMouseReleasedAction(viewTerritoriesAction);


            GameAction viewRaceTerritoryAction = new ViewRaceTerritoryGameAction(mapEditorStage, md, newMap,
                    soundManager.getSound((int)SOUNDS.BASE_UPGRADE));
            Button viewRaceTerritoryButton = new Button(797, 466, new Sprite(textures["spr_viewraceTerritoryButton"], 96, 32, 1, 3));
            mapEditorStage.add(viewRaceTerritoryButton);
            viewRaceTerritoryButton.setMouseReleasedAction(viewRaceTerritoryAction);


    ///////////



            GameAction saveMapAndContinueAction = new SaveMapGameAction(mapManager, newMap, this,
                    soundManager.getSound((int)SOUNDS.END_TURN));
            Button saveButton = new Button(700, 500, new Sprite(textures["spr_saveMapButton"], 96, 32, 1, 3));
            mapEditorStage.add(saveButton);
            saveButton.setMouseReleasedAction(saveMapAndContinueAction);

        }

        private void buildIPAddressMenu()
        {
            //call update for keyboard, on update, -> MenuOption.setName = string -> once enter pressed->call enter action to profileManager.createProfile
            x = GraphicsDevice.Viewport.Width / 2;

            MenuStage enterIPMenu =
                new MenuStage("Enter host's IP Address to join game.", fonts["menuFont"], x, 0,
                    soundManager.getSound((int)SOUNDS.MENU_CHANGE));

            enterIPMenu.setBackground(textures["bg_matrix"]);

            ipAddress = new MenuOption(spriteBatch, x, y + 100, "_", fonts["menuFont"], true);
            ipAddress.setEnterAction(new SubmitIPAddressGameAction(this));
            enterIPMenu.addOption(ipAddress);

            MenuOption backOption = new MenuOption(spriteBatch, x, y + 150, "Back", fonts["menuFont"], true);
            backOption.setEnterAction(new MenuGameAction(this, (int)STAGES.GAME_LOBBY, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            enterIPMenu.addOption(backOption);

            stages.Insert((int)STAGES.ENTER_IP_ADDRESS, enterIPMenu);
        }

        private void buildGameOverStage()
        {
            Stage gameOverStage = new Stage();

            gameOverMessage = new TextComponent(fonts["menuFont"],300,200);

            Button restartButton = new Button(500,500, new Sprite(textures["spr_turnButton"], 96, 32, 1, 3));
            restartButton.setMouseReleasedAction(
                new MenuGameAction(this, (int)STAGES.MAIN_MENU, soundManager.getSound((int) SOUNDS.MENU_SELECT)));

            gameOverStage.add(gameOverMessage);
            gameOverStage.add(restartButton);
            stages.Add(gameOverStage);
        }

        internal void buildPlayStage()
        {
            profileManager.initializeSelection(gameSettings.getNumPlayers());
            profileManager.createProfile("FOOGLE");
            profileManager.createProfile("BARBLE");
            //Just in case profiles have already been created
            profileManager.selectProfile(0, 0);
            profileManager.selectProfile(1, 1);
            profileManager.finalize();
            if (isNetworked)
            {
                if (isHost)
                {
                    game = new NetworkedGameHost(gameSettings, board);
                    ((NetworkedGameHost)game).sendGameData(mapManager.getMap());
                }
                else
                {
                    game = new NetworkedGameClient(gameSettings, ipAddress.menuName);
                    ((NetworkedGameClient)game).receiveGameData();
                }
            }
            else
            {
                this.game = new Game(gameSettings, board);
            }
            UpgradeMenuComposite upgradeMenu = new UpgradeMenuComposite();
            PlayStage playStage = new PlayStage(upgradeMenu, this, textures["drag_rectangle"],game);

            Sprite tileSprite = new Sprite(textures["spr_tiles"], 32, 32, 9, 18);
            
           
            Button upgrade = new Button(0, 0);
            upgrade.setSprite(new Sprite(textures["spr_speedButton"], 64, 64, 4, 4));
            upgrade.setMousePressedAction(
                new UpgradeSpeedGameAction(this, playStage, 
                soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            upgradeMenu.add(upgrade);

            upgrade = new Button(64, 0);
            upgrade.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 4, 4));
            upgrade.setMousePressedAction(
                new UpgradePotency1GameAction(this, playStage, 
                    soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            upgradeMenu.add(upgrade);

            upgrade = new Button(128, 0);
            upgrade.setSprite(new Sprite(textures["spr_potencyButton3"], 64, 64, 4, 4));
            upgrade.setMousePressedAction(
                new UpgradePotency3GameAction(this, playStage,
                    soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            upgradeMenu.add(upgrade);

            upgrade = new Button(192, 0);
            upgrade.setSprite(new Sprite(textures["spr_outbreakButton"], 64, 64, 4, 4));
            upgrade.setMousePressedAction(
                new UpgradeOutbreakGameAction(this, playStage,
                    soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            upgradeMenu.add(upgrade);

            upgrade = new Button(256, 0);
            upgrade.setSprite(new Sprite(textures["spr_resilienceButton"], 64, 64, 4, 4));
            upgrade.setMousePressedAction(
                new UpgradeResilienceGameAction(this, playStage,
                    soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            upgradeMenu.add(upgrade);

            upgrade = new Button(320, 0);
            upgrade.setSprite(new Sprite(textures["spr_baseButton"], 64, 64, 4, 4));
            upgrade.setMousePressedAction(
                new UpgradeBaseGameAction(this, playStage, 
                    soundManager.getSound((int)SOUNDS.BASE_UPGRADE)));
            upgradeMenu.add(upgrade);

            PotencyBar pb = new PotencyBar(this, 20, 500, new Sprite(textures["spr_plain"]), fonts["font_potencyBar"], profileManager);
            playStage.add(pb);

            HudComponent hud = new HudComponent(playStage, fonts["font_potencyBar"], 800, 200, new Sprite(textures["spr_tilesBig"],64,64,9,9));
            playStage.add(hud);

            /*****
             * REFACTOR THIS AHHHHHHHHHHHHHHHHH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
             * 
             * 
             * 
             *****/
            TextComponent title = new TextComponent(fonts["font_potencyBar"], 400, 10);
            switch( gameSettings.getGameType() )
            {/*ENDURANCE,
               TERRITORIES,
               QUICK_WIN,
               TERRITORY_RACE
              /*/
                case 0:
                    title.setString("Game Type: Endurance");
                    break;
                case 1:
                    title.setString("Game Type: Territories");
                    break;
                case 2:
                    title.setString("Game Type: Quick Win");
                    break;
                case 3:
                    title.setString("Game Type: Territory Race");
                    break;
            }
            playStage.add(title);
            
            playerScores = new TextComponent(fonts["font_potencyBar"],10,10);
            playStage.add(playerScores);
            
            #region alliance buttons

            /*
             *  This is for Alliance buttons 
             * 
             * 
             * 
             * 
             * 
             *

            int b0x = 100;
            int b1x = 250;
            int b2x = 400;
            int b3x = 550;
            int by = 600;

            Color p0c = Color.Orange;
            Color p1c = Color.Purple;
            Color p2c = Color.Blue;
            Color p3c = Color.Green;

            sendRequestButton01 = new AllianceButton(b1x, by, new Sprite(textures["spr_requestButton"], 128, 64, 4, 4), p1c);
            sendRequestButton01.setMouseReleasedAction(new SendAllianceRequestGameAction(this, game, 1));
            sendRequestButton02 = new AllianceButton(b2x, by, new Sprite(textures["spr_requestButton"], 128, 64, 4, 4), p2c);
            sendRequestButton02.setMouseReleasedAction(new SendAllianceRequestGameAction(this, game, 2));
            sendRequestButton03 = new AllianceButton(b3x, by, new Sprite(textures["spr_requestButton"], 128, 64, 4, 4), p3c);
            sendRequestButton03.setMouseReleasedAction(new SendAllianceRequestGameAction(this, game, 3));
            sendRequestButton10 = new AllianceButton(b0x, by, new Sprite(textures["spr_requestButton"], 128, 64, 4, 4), p0c);
            sendRequestButton10.setMouseReleasedAction(new SendAllianceRequestGameAction(this, game, 0));
            sendRequestButton12 = new AllianceButton(b2x, by, new Sprite(textures["spr_requestButton"], 128, 64, 4, 4), p2c);
            sendRequestButton12.setMouseReleasedAction(new SendAllianceRequestGameAction(this, game, 2));
            sendRequestButton13 = new AllianceButton(b3x, by, new Sprite(textures["spr_requestButton"], 128, 64, 4, 4), p3c);
            sendRequestButton13.setMouseReleasedAction(new SendAllianceRequestGameAction(this, game, 3));
            sendRequestButton20 = new AllianceButton(b0x, by, new Sprite(textures["spr_requestButton"], 128, 64, 4, 4), p0c);
            sendRequestButton20.setMouseReleasedAction(new SendAllianceRequestGameAction(this, game, 0));
            sendRequestButton21 = new AllianceButton(b1x, by, new Sprite(textures["spr_requestButton"], 128, 64, 4, 4), p1c);
            sendRequestButton21.setMouseReleasedAction(new SendAllianceRequestGameAction(this, game, 1));
            sendRequestButton23 = new AllianceButton(b3x, by, new Sprite(textures["spr_requestButton"], 128, 64, 4, 4), p3c);
            sendRequestButton23.setMouseReleasedAction(new SendAllianceRequestGameAction(this, game, 3));
            sendRequestButton30 = new AllianceButton(b0x, by, new Sprite(textures["spr_requestButton"], 128, 64, 4, 4), p0c);
            sendRequestButton30.setMouseReleasedAction(new SendAllianceRequestGameAction(this, game, 0));
            sendRequestButton31 = new AllianceButton(b1x, by, new Sprite(textures["spr_requestButton"], 128, 64, 4, 4), p1c);
            sendRequestButton31.setMouseReleasedAction(new SendAllianceRequestGameAction(this, game, 1));
            sendRequestButton32 = new AllianceButton(b2x, by, new Sprite(textures["spr_requestButton"], 128, 64, 4, 4), p2c);
            sendRequestButton32.setMouseReleasedAction(new SendAllianceRequestGameAction(this, game, 2));

            sendRequestButton01.visible = false;
            sendRequestButton02.visible = false;
            sendRequestButton03.visible = false;
            sendRequestButton10.visible = false;
            sendRequestButton12.visible = false;
            sendRequestButton13.visible = false;
            sendRequestButton20.visible = false;
            sendRequestButton21.visible = false;
            sendRequestButton23.visible = false;
            sendRequestButton30.visible = false;
            sendRequestButton31.visible = false;
            sendRequestButton32.visible = false;

            playStage.add(sendRequestButton01);
            playStage.add(sendRequestButton02);
            playStage.add(sendRequestButton03);
            playStage.add(sendRequestButton10);
            playStage.add(sendRequestButton12);
            playStage.add(sendRequestButton13);
            playStage.add(sendRequestButton20);
            playStage.add(sendRequestButton21);
            playStage.add(sendRequestButton23);
            playStage.add(sendRequestButton30);
            playStage.add(sendRequestButton31);
            playStage.add(sendRequestButton32);

            acceptRequestButton01 = new AllianceButton(b1x, by, new Sprite(textures["spr_acceptButton"], 128, 64, 4, 4), p1c);
            acceptRequestButton01.setMouseReleasedAction(new AcceptAllianceRequestGameAction(this, game, 0));
            acceptRequestButton02 = new AllianceButton(b2x, by, new Sprite(textures["spr_acceptButton"], 128, 64, 4, 4), p2c);
            acceptRequestButton02.setMouseReleasedAction(new AcceptAllianceRequestGameAction(this, game, 0));
            acceptRequestButton03 = new AllianceButton(b3x, by, new Sprite(textures["spr_acceptButton"], 128, 64, 4, 4), p3c);
            acceptRequestButton03.setMouseReleasedAction(new AcceptAllianceRequestGameAction(this, game, 0));
            acceptRequestButton10 = new AllianceButton(b0x, by, new Sprite(textures["spr_acceptButton"], 128, 64, 4, 4), p0c);
            acceptRequestButton10.setMouseReleasedAction(new AcceptAllianceRequestGameAction(this, game, 1));
            acceptRequestButton12 = new AllianceButton(b2x, by, new Sprite(textures["spr_acceptButton"], 128, 64, 4, 4), p2c);
            acceptRequestButton12.setMouseReleasedAction(new AcceptAllianceRequestGameAction(this, game, 1));
            acceptRequestButton13 = new AllianceButton(b3x, by, new Sprite(textures["spr_acceptButton"], 128, 64, 4, 4), p3c);
            acceptRequestButton13.setMouseReleasedAction(new AcceptAllianceRequestGameAction(this, game, 1));
            acceptRequestButton20 = new AllianceButton(b0x, by, new Sprite(textures["spr_acceptButton"], 128, 64, 4, 4), p0c);
            acceptRequestButton20.setMouseReleasedAction(new AcceptAllianceRequestGameAction(this, game, 2));
            acceptRequestButton21 = new AllianceButton(b1x, by, new Sprite(textures["spr_acceptButton"], 128, 64, 4, 4), p1c);
            acceptRequestButton21.setMouseReleasedAction(new AcceptAllianceRequestGameAction(this, game, 2));
            acceptRequestButton23 = new AllianceButton(b3x, by, new Sprite(textures["spr_acceptButton"], 128, 64, 4, 4), p3c);
            acceptRequestButton23.setMouseReleasedAction(new AcceptAllianceRequestGameAction(this, game, 2));
            acceptRequestButton30 = new AllianceButton(b0x, by, new Sprite(textures["spr_acceptButton"], 128, 64, 4, 4), p0c);
            acceptRequestButton30.setMouseReleasedAction(new AcceptAllianceRequestGameAction(this, game, 3));
            acceptRequestButton31 = new AllianceButton(b1x, by, new Sprite(textures["spr_acceptButton"], 128, 64, 4, 4), p1c);
            acceptRequestButton31.setMouseReleasedAction(new AcceptAllianceRequestGameAction(this, game, 3));
            acceptRequestButton32 = new AllianceButton(b2x, by, new Sprite(textures["spr_acceptButton"], 128, 64, 4, 4), p2c);
            acceptRequestButton32.setMouseReleasedAction(new AcceptAllianceRequestGameAction(this, game, 3));

            acceptRequestButton01.visible = false;
            acceptRequestButton02.visible = false;
            acceptRequestButton03.visible = false;
            acceptRequestButton10.visible = false;
            acceptRequestButton12.visible = false;
            acceptRequestButton13.visible = false;
            acceptRequestButton20.visible = false;
            acceptRequestButton21.visible = false;
            acceptRequestButton23.visible = false;
            acceptRequestButton30.visible = false;
            acceptRequestButton31.visible = false;
            acceptRequestButton32.visible = false;

            playStage.add(acceptRequestButton01);
            playStage.add(acceptRequestButton02);
            playStage.add(acceptRequestButton03);
            playStage.add(acceptRequestButton10);
            playStage.add(acceptRequestButton12);
            playStage.add(acceptRequestButton13);
            playStage.add(acceptRequestButton20);
            playStage.add(acceptRequestButton21);
            playStage.add(acceptRequestButton23);
            playStage.add(acceptRequestButton30);
            playStage.add(acceptRequestButton31);
            playStage.add(acceptRequestButton32);

            cancelRequestButton01 = new AllianceButton(b1x, by, new Sprite(textures["spr_waitingCancelButton"], 128, 64, 4, 4), p1c);
            cancelRequestButton01.setMouseReleasedAction(new CancelAllianceRequestGameAction(this, game, 1));
            cancelRequestButton01.setActive(false);
            cancelRequestButton02 = new AllianceButton(b2x, by, new Sprite(textures["spr_waitingCancelButton"], 128, 64, 4, 4), p2c);
            cancelRequestButton02.setMouseReleasedAction(new CancelAllianceRequestGameAction(this, game, 2));
            cancelRequestButton02.setActive(false);
            cancelRequestButton03 = new AllianceButton(b3x, by, new Sprite(textures["spr_waitingCancelButton"], 128, 64, 4, 4), p3c);
            cancelRequestButton03.setMouseReleasedAction(new CancelAllianceRequestGameAction(this, game, 3));
            cancelRequestButton03.setActive(false);
            cancelRequestButton10 = new AllianceButton(b0x, by, new Sprite(textures["spr_waitingCancelButton"], 128, 64, 4, 4), p0c);
            cancelRequestButton10.setMouseReleasedAction(new CancelAllianceRequestGameAction(this, game, 0));
            cancelRequestButton10.setActive(false);
            cancelRequestButton12 = new AllianceButton(b2x, by, new Sprite(textures["spr_waitingCancelButton"], 128, 64, 4, 4), p2c);
            cancelRequestButton12.setMouseReleasedAction(new CancelAllianceRequestGameAction(this, game, 2));
            cancelRequestButton12.setActive(false);
            cancelRequestButton13 = new AllianceButton(b3x, by, new Sprite(textures["spr_waitingCancelButton"], 128, 64, 4, 4), p3c);
            cancelRequestButton13.setMouseReleasedAction(new CancelAllianceRequestGameAction(this, game, 3));
            cancelRequestButton13.setActive(false);
            cancelRequestButton20 = new AllianceButton(b0x, by, new Sprite(textures["spr_waitingCancelButton"], 128, 64, 4, 4), p0c);
            cancelRequestButton20.setMouseReleasedAction(new CancelAllianceRequestGameAction(this, game, 0));
            cancelRequestButton20.setActive(false);
            cancelRequestButton21 = new AllianceButton(b1x, by, new Sprite(textures["spr_waitingCancelButton"], 128, 64, 4, 4), p1c);
            cancelRequestButton21.setMouseReleasedAction(new CancelAllianceRequestGameAction(this, game, 1));
            cancelRequestButton21.setActive(false);
            cancelRequestButton23 = new AllianceButton(b3x, by, new Sprite(textures["spr_waitingCancelButton"], 128, 64, 4, 4), p3c);
            cancelRequestButton23.setMouseReleasedAction(new CancelAllianceRequestGameAction(this, game, 3));
            cancelRequestButton23.setActive(false);
            cancelRequestButton30 = new AllianceButton(b0x, by, new Sprite(textures["spr_waitingCancelButton"], 128, 64, 4, 4), p0c);
            cancelRequestButton30.setMouseReleasedAction(new CancelAllianceRequestGameAction(this, game, 0));
            cancelRequestButton30.setActive(false);
            cancelRequestButton31 = new AllianceButton(b1x, by, new Sprite(textures["spr_waitingCancelButton"], 128, 64, 4, 4), p1c);
            cancelRequestButton31.setMouseReleasedAction(new CancelAllianceRequestGameAction(this, game, 1));
            cancelRequestButton31.setActive(false);
            cancelRequestButton32 = new AllianceButton(b2x, by, new Sprite(textures["spr_waitingCancelButton"], 128, 64, 4, 4), p2c);
            cancelRequestButton32.setMouseReleasedAction(new CancelAllianceRequestGameAction(this, game, 2));
            cancelRequestButton32.setActive(false);

            cancelRequestButton01.visible = false;
            cancelRequestButton02.visible = false;
            cancelRequestButton03.visible = false;
            cancelRequestButton10.visible = false;
            cancelRequestButton12.visible = false;
            cancelRequestButton13.visible = false;
            cancelRequestButton20.visible = false;
            cancelRequestButton21.visible = false;
            cancelRequestButton23.visible = false;
            cancelRequestButton30.visible = false;
            cancelRequestButton31.visible = false;
            cancelRequestButton32.visible = false;

            playStage.add(cancelRequestButton01);
            playStage.add(cancelRequestButton02);
            playStage.add(cancelRequestButton03);
            playStage.add(cancelRequestButton10);
            playStage.add(cancelRequestButton12);
            playStage.add(cancelRequestButton13);
            playStage.add(cancelRequestButton20);
            playStage.add(cancelRequestButton21);
            playStage.add(cancelRequestButton23);
            playStage.add(cancelRequestButton30);
            playStage.add(cancelRequestButton31);
            playStage.add(cancelRequestButton32);

            breakAllianceButton01 = new AllianceButton(b1x, by, new Sprite(textures["spr_breakButton"], 128, 64, 4, 4), p1c);
            breakAllianceButton01.setMouseReleasedAction(new BreakAllianceGameAction(this, game, 1));
            breakAllianceButton02 = new AllianceButton(b2x, by, new Sprite(textures["spr_breakButton"], 128, 64, 4, 4), p2c);
            breakAllianceButton02.setMouseReleasedAction(new BreakAllianceGameAction(this, game, 2));
            breakAllianceButton03 = new AllianceButton(b3x, by, new Sprite(textures["spr_breakButton"], 128, 64, 4, 4), p3c);
            breakAllianceButton03.setMouseReleasedAction(new BreakAllianceGameAction(this, game, 3));
            breakAllianceButton10 = new AllianceButton(b0x, by, new Sprite(textures["spr_breakButton"], 128, 64, 4, 4), p0c);
            breakAllianceButton10.setMouseReleasedAction(new BreakAllianceGameAction(this, game, 0));
            breakAllianceButton12 = new AllianceButton(b2x, by, new Sprite(textures["spr_breakButton"], 128, 64, 4, 4), p2c);
            breakAllianceButton12.setMouseReleasedAction(new BreakAllianceGameAction(this, game, 2));
            breakAllianceButton13 = new AllianceButton(b3x, by, new Sprite(textures["spr_breakButton"], 128, 64, 4, 4), p3c);
            breakAllianceButton13.setMouseReleasedAction(new BreakAllianceGameAction(this, game, 3));
            breakAllianceButton20 = new AllianceButton(b0x, by, new Sprite(textures["spr_breakButton"], 128, 64, 4, 4), p0c);
            breakAllianceButton20.setMouseReleasedAction(new BreakAllianceGameAction(this, game, 0));
            breakAllianceButton21 = new AllianceButton(b1x, by, new Sprite(textures["spr_breakButton"], 128, 64, 4, 4), p1c);
            breakAllianceButton21.setMouseReleasedAction(new BreakAllianceGameAction(this, game, 1));
            breakAllianceButton23 = new AllianceButton(b3x, by, new Sprite(textures["spr_breakButton"], 128, 64, 4, 4), p3c);
            breakAllianceButton23.setMouseReleasedAction(new BreakAllianceGameAction(this, game, 3));
            breakAllianceButton30 = new AllianceButton(b0x, by, new Sprite(textures["spr_breakButton"], 128, 64, 4, 4), p0c);
            breakAllianceButton30.setMouseReleasedAction(new BreakAllianceGameAction(this, game, 0));
            breakAllianceButton31 = new AllianceButton(b1x, by, new Sprite(textures["spr_breakButton"], 128, 64, 4, 4), p1c);
            breakAllianceButton31.setMouseReleasedAction(new BreakAllianceGameAction(this, game, 1));
            breakAllianceButton32 = new AllianceButton(b2x, by, new Sprite(textures["spr_breakButton"], 128, 64, 4, 4), p2c);
            breakAllianceButton32.setMouseReleasedAction(new BreakAllianceGameAction(this, game, 2));


            breakAllianceButton01.visible = false;
            breakAllianceButton02.visible = false;
            breakAllianceButton03.visible = false;
            breakAllianceButton10.visible = false;
            breakAllianceButton12.visible = false;
            breakAllianceButton13.visible = false;
            breakAllianceButton20.visible = false;
            breakAllianceButton21.visible = false;
            breakAllianceButton23.visible = false;
            breakAllianceButton30.visible = false;
            breakAllianceButton31.visible = false;
            breakAllianceButton32.visible = false;

            playStage.add(breakAllianceButton01);
            playStage.add(breakAllianceButton02);
            playStage.add(breakAllianceButton03);
            playStage.add(breakAllianceButton10);
            playStage.add(breakAllianceButton12);
            playStage.add(breakAllianceButton13);
            playStage.add(breakAllianceButton20);
            playStage.add(breakAllianceButton21);
            playStage.add(breakAllianceButton23);
            playStage.add(breakAllianceButton30);
            playStage.add(breakAllianceButton31);
            playStage.add(breakAllianceButton32);
            /**/
            #endregion


            List<List<GameboardCell>> temp = new List<List<GameboardCell>>();

            int offsetY = 0;
            int offsetX = 0;
            foreach (DataRow row in game.board.getBoard().Rows) // Loop over the rows.
            {
                offsetX = 0;
                List<GameboardCell> temp2 = new List<GameboardCell>();
                foreach (Tile tile in row.ItemArray) // Loop over the items.
                {
                    GameboardCell cell = new GameboardCell(game, profileManager, tile, tileSprite, offsetX, offsetY);
                    cell.setMousePressedAction(
                        new TileClickGameAction(game, playStage, cell, 
                            soundManager.getSound((int)SOUNDS.TILE_CLICK)));
                    temp2.Add(cell);
                    offsetX++;
                }
                temp.Add(temp2);
                offsetY++;
            }
            GameboardComponent gbc;
            int w = game.board.getBoard().Columns.Count;
            int h = game.board.getBoard().Rows.Count;
            if (w > h)
            {
                double ratio = (double)h / (double)w;
                gbc = new GameboardComponent(temp, game, 250, 64, 500, (int)(500 * ratio), offsetX, offsetY, new Sprite(textures["spr_numbers"], 16, 16, 20, 20));
            }
            else
            {

                double ratio = (double)w / (double)h;
                gbc = new GameboardComponent(temp, game, 250, 64, (int)(500 * ratio), 500, offsetX, offsetY, new Sprite(textures["spr_numbers"], 16, 16, 20, 20));
            }
            playStage.setGameboard(gbc);

            TextComponent timeDrawer = new TimeDrawer(this.game, fonts["font_potencyBar"],10, 550);
            playStage.add(timeDrawer);

            /*offsetY = 0;
            offsetX = 0;
            temp.Clear();
            game.createFutureGame();
            Game futureGame = game.futureGame;
            foreach (DataRow row in futureGame.board.getBoard().Rows) // Loop over the rows.
            {
                offsetX = 0;
                List<GameboardCell> temp2 = new List<GameboardCell>();
                foreach (Tile tile in row.ItemArray) // Loop over the items.
                {
                    GameboardCell cell = new GameboardCell(game, tile, tileSprite, offsetX, offsetY);
                    cell.setMousePressedAction(new TileClickGameAction(game, playStage, cell));
                    temp2.Add(cell);
                    offsetX++;
                }
                temp.Add(temp2);
                offsetY++;
            }
            GameboardComponent gbc2 = new GameboardComponent(temp, 775, 200, 225, 225, offsetX, offsetY, new Sprite(textures["spr_numbers"], 1, 1, 1, 1));
            playStage.add(gbc2);
            /**/
            Button turnButton = new Button(100, 468, new Sprite(textures["spr_turnButton"], 96, 32, 1, 3));
            if (isNetworked)
            {
                turnButton.setMouseReleasedAction(
                    new EndTurnGameAction(this, new NextStageGameAction(this),
                        soundManager.getSound((int)SOUNDS.END_TURN)));
            }
            else
            {
                turnButton.setMouseReleasedAction(
                    new ChangeTurnGameAction(this, new NextStageGameAction(this),
                        soundManager.getSound((int)SOUNDS.END_TURN)));
            } 
            playStage.add(turnButton);

            playStageIndex = stages.Count();
            stages.Add(playStage);


            Stage interemStage = new Stage();
            Button startTurnButton = new Button(120,468, new Sprite(textures["spr_startTurnButton"], 96,32,1,3));
            TextComponent nextPlayersTurnText = new TextComponent(fonts["font_potencyBar"],200,200);
            nextPlayersTurnText.setString("It's the next player's turn!");
            startTurnButton.setMouseReleasedAction(new PreviousStageGameAction(this));
            interemStage.add(startTurnButton);
            interemStage.add(nextPlayersTurnText);
            stages.Add(interemStage);
        }

        private void buildTutorialStages()
        {
            //Frizzell!!!
            Button continueButton = new Button(825, 200);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int) SOUNDS.MENU_SELECT)));
            Stage tutorialStage001 = TutorialFactory.getTutorial(textures["bg_tut001"], continueButton);
            stages.Add(tutorialStage001);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage0011 = TutorialFactory.getTutorial(textures["bg_tut001.1"], continueButton);
            stages.Add(tutorialStage0011);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage002 = TutorialFactory.getTutorial(textures["bg_tut002"], continueButton);
            stages.Add(tutorialStage002);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage003 = TutorialFactory.getTutorial(textures["bg_tut003"], continueButton);
            stages.Add(tutorialStage003);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage004 = TutorialFactory.getTutorial(textures["bg_tut004"], continueButton);
            stages.Add(tutorialStage004);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage005 = TutorialFactory.getTutorial(textures["bg_tut005"], continueButton);
            stages.Add(tutorialStage005);

            Button placeBaseButton = new Button(280, 486);

            placeBaseButton.setSprite(new Sprite(textures["spr_placeBaseButton"], 25, 25, 2, 1));
            placeBaseButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage006 = TutorialFactory.getTutorial(textures["bg_tut006"], placeBaseButton);
            stages.Add(tutorialStage006);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage007 = TutorialFactory.getTutorial(textures["bg_tut007"], continueButton);
            stages.Add(tutorialStage007);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage008 = TutorialFactory.getTutorial(textures["bg_tut008"], continueButton);
            stages.Add(tutorialStage008);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage009 = TutorialFactory.getTutorial(textures["bg_tut009"], continueButton);
            stages.Add(tutorialStage009);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage010 = TutorialFactory.getTutorial(textures["bg_tut010"], continueButton);
            stages.Add(tutorialStage010);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage011 = TutorialFactory.getTutorial(textures["bg_tut011"], continueButton);
            stages.Add(tutorialStage011);

            Button turnButton = new Button(93, 454);

            turnButton.setSprite(new Sprite(textures["spr_turnButton"], 96, 32, 1, 3));
            turnButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.END_TURN)));
            Stage tutorialStage012 = TutorialFactory.getTutorial(textures["bg_tut012"], turnButton);
            stages.Add(tutorialStage012);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage013 = TutorialFactory.getTutorial(textures["bg_tut013"], continueButton);
            stages.Add(tutorialStage013);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage014 = TutorialFactory.getTutorial(textures["bg_tut014"], continueButton);
            stages.Add(tutorialStage014);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage015 = TutorialFactory.getTutorial(textures["bg_tut015"], continueButton);
            stages.Add(tutorialStage015);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage016 = TutorialFactory.getTutorial(textures["bg_tut016"], continueButton);
            stages.Add(tutorialStage016);

            Button upgradeButton1 = new Button(299, 486);

            upgradeButton1.setSprite(new Sprite(textures["spr_upgradeButton1"], 26, 26, 2, 1));
            upgradeButton1.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage017 = TutorialFactory.getTutorial(textures["bg_tut017"], upgradeButton1);
            stages.Add(tutorialStage017);

            Button speedButton = new Button(121, 426);
            speedButton.setSprite(new Sprite(textures["spr_speedButton"], 64, 64, 2, 4));
            speedButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage018 = TutorialFactory.getTutorial(textures["bg_tut018"], speedButton);
            stages.Add(tutorialStage018);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage019 = TutorialFactory.getTutorial(textures["bg_tut019"], continueButton);
            stages.Add(tutorialStage019);

            Button potencyButton1 = new Button(185, 426);

            potencyButton1.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton1.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0200 = TutorialFactory.getTutorial(textures["bg_tut020.0"], potencyButton1);
            stages.Add(tutorialStage0200);

            potencyButton1.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton1.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0201 = TutorialFactory.getTutorial(textures["bg_tut020.1"], potencyButton1);
            stages.Add(tutorialStage0201);

            potencyButton1.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton1.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0202 = TutorialFactory.getTutorial(textures["bg_tut020.2"], potencyButton1);
            stages.Add(tutorialStage0202);

            potencyButton1.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton1.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0203 = TutorialFactory.getTutorial(textures["bg_tut020.3"], potencyButton1);
            stages.Add(tutorialStage0203);

            potencyButton1.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton1.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0204 = TutorialFactory.getTutorial(textures["bg_tut020.4"], potencyButton1);
            stages.Add(tutorialStage0204);

            potencyButton1.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton1.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0205 = TutorialFactory.getTutorial(textures["bg_tut020.5"], potencyButton1);
            stages.Add(tutorialStage0205);

            potencyButton1.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton1.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0206 = TutorialFactory.getTutorial(textures["bg_tut020.6"], potencyButton1);
            stages.Add(tutorialStage0206);

            potencyButton1.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton1.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0207 = TutorialFactory.getTutorial(textures["bg_tut020.7"], potencyButton1);
            stages.Add(tutorialStage0207);

            turnButton.setSprite(new Sprite(textures["spr_turnButton"], 96, 32, 1, 3));
            turnButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.END_TURN)));
            Stage tutorialStage021 = TutorialFactory.getTutorial(textures["bg_tut021"], turnButton);
            stages.Add(tutorialStage021);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage022 = TutorialFactory.getTutorial(textures["bg_tut022"], continueButton);
            stages.Add(tutorialStage022);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage023 = TutorialFactory.getTutorial(textures["bg_tut023"], continueButton);
            stages.Add(tutorialStage023);

            Button upgradeButton2 = new Button(338, 487);
            upgradeButton2.setSprite(new Sprite(textures["spr_upgradeButton2"], 26, 26, 2, 1));
            upgradeButton2.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage024 = TutorialFactory.getTutorial(textures["bg_tut024"], upgradeButton2);
            stages.Add(tutorialStage024);

            Button outbreakButton = new Button(352, 426);

            outbreakButton.setSprite(new Sprite(textures["spr_outbreakButton"], 64, 64, 2, 4));
            outbreakButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage025 = TutorialFactory.getTutorial(textures["bg_tut025"], outbreakButton);
            stages.Add(tutorialStage025);

            Button potencyButton10 = new Button(225, 426);

            potencyButton10.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton10.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage026 = TutorialFactory.getTutorial(textures["bg_tut026"], potencyButton10);
            stages.Add(tutorialStage026);

            potencyButton10.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton10.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage027 = TutorialFactory.getTutorial(textures["bg_tut027"], potencyButton10);
            stages.Add(tutorialStage027);

            potencyButton10.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton10.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage028 = TutorialFactory.getTutorial(textures["bg_tut028"], potencyButton10);
            stages.Add(tutorialStage028);

            turnButton.setSprite(new Sprite(textures["spr_turnButton"], 96, 32, 1, 3));
            turnButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.END_TURN)));
            Stage tutorialStage029 = TutorialFactory.getTutorial(textures["bg_tut029"], turnButton);
            stages.Add(tutorialStage029);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage030 = TutorialFactory.getTutorial(textures["bg_tut030"], continueButton);
            stages.Add(tutorialStage030);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage031 = TutorialFactory.getTutorial(textures["bg_tut031"], continueButton);
            stages.Add(tutorialStage031);

            Button upgradeButton20 = new Button(380, 466);

            upgradeButton20.setSprite(new Sprite(textures["spr_upgradeButton2"], 26, 26, 2, 1));
            upgradeButton20.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage032 = TutorialFactory.getTutorial(textures["bg_tut032"], upgradeButton20);
            stages.Add(tutorialStage032);

            Button speedButton1 = new Button(200, 406);
            speedButton1.setSprite(new Sprite(textures["spr_speedButton"], 64, 64, 2, 4));
            speedButton1.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage033 = TutorialFactory.getTutorial(textures["bg_tut033"], speedButton1);
            stages.Add(tutorialStage033);

            speedButton1.setSprite(new Sprite(textures["spr_speedButton"], 64, 64, 2, 4));
            speedButton1.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage034 = TutorialFactory.getTutorial(textures["bg_tut034"], speedButton1);
            stages.Add(tutorialStage034);

            turnButton.setSprite(new Sprite(textures["spr_turnButton"], 96, 32, 1, 3));
            turnButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.END_TURN)));
            Stage tutorialStage035 = TutorialFactory.getTutorial(textures["bg_tut035"], turnButton);
            stages.Add(tutorialStage035);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage036 = TutorialFactory.getTutorial(textures["bg_tut036"], continueButton);
            stages.Add(tutorialStage036);

            Button upgradeButton3 = new Button(380, 405);
            upgradeButton3.setSprite(new Sprite(textures["spr_upgradeButton3"], 26, 26, 2, 1));
            upgradeButton3.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage037 = TutorialFactory.getTutorial(textures["bg_tut037"], upgradeButton3);
            stages.Add(tutorialStage037);

            Button potencyButton11 = new Button(265, 346);
            potencyButton11.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton11.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage038 = TutorialFactory.getTutorial(textures["bg_tut038"], potencyButton11);
            stages.Add(tutorialStage038);

            potencyButton11.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton11.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage039 = TutorialFactory.getTutorial(textures["bg_tut039"], potencyButton11);
            stages.Add(tutorialStage039);

            potencyButton11.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton11.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage040 = TutorialFactory.getTutorial(textures["bg_tut040"], potencyButton11);
            stages.Add(tutorialStage040);

            potencyButton11.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton11.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage041 = TutorialFactory.getTutorial(textures["bg_tut041"], potencyButton11);
            stages.Add(tutorialStage041);

            potencyButton11.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton11.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage042 = TutorialFactory.getTutorial(textures["bg_tut042"], potencyButton11);
            stages.Add(tutorialStage042);

            potencyButton11.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton11.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage043 = TutorialFactory.getTutorial(textures["bg_tut043"], potencyButton11);
            stages.Add(tutorialStage043);

            potencyButton11.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton11.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage044 = TutorialFactory.getTutorial(textures["bg_tut044"], potencyButton11);
            stages.Add(tutorialStage044);

            turnButton.setSprite(new Sprite(textures["spr_turnButton"], 96, 32, 1, 3));
            turnButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.END_TURN)));
            Stage tutorialStage045 = TutorialFactory.getTutorial(textures["bg_tut045"], turnButton);
            stages.Add(tutorialStage045);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage046 = TutorialFactory.getTutorial(textures["bg_tut046"], continueButton);
            stages.Add(tutorialStage046);

            Button upgradeButton40 = new Button(380, 388);
            upgradeButton40.setSprite(new Sprite(textures["spr_upgradeButton4"], 24, 21, 1, 1));
            upgradeButton40.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage047 = TutorialFactory.getTutorial(textures["bg_tut047"], upgradeButton40);
            stages.Add(tutorialStage047);

            Button baseButton = new Button(522, 326);
            baseButton.setSprite(new Sprite(textures["spr_baseButton"], 64, 64, 4, 4));
            baseButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.BASE_UPGRADE)));
            Stage tutorialStage048 = TutorialFactory.getTutorial(textures["bg_tut048"], baseButton);
            stages.Add(tutorialStage048);

            turnButton.setSprite(new Sprite(textures["spr_turnButton"], 96, 32, 1, 3));
            turnButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.END_TURN)));
            Stage tutorialStage049 = TutorialFactory.getTutorial(textures["bg_tut049"], turnButton);
            stages.Add(tutorialStage049);

            Button upgradeButton41 = new Button(580, 190);
            upgradeButton41.setSprite(new Sprite(textures["spr_upgradeButton4"], 24, 21, 1, 1));
            upgradeButton41.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage050 = TutorialFactory.getTutorial(textures["bg_tut050"], upgradeButton41);
            stages.Add(tutorialStage050);

            Button outbreakButton1 = new Button(593, 126);
            outbreakButton1.setSprite(new Sprite(textures["spr_outbreakButton"], 64, 64, 2, 4));
            outbreakButton1.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage051 = TutorialFactory.getTutorial(textures["bg_tut051"], outbreakButton1);
            stages.Add(tutorialStage051);


            Button speedButton2 = new Button(401, 126);
            speedButton2.setSprite(new Sprite(textures["spr_speedButton"], 64, 64, 2, 4));
            speedButton2.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage052 = TutorialFactory.getTutorial(textures["bg_tut052"], speedButton2);
            stages.Add(tutorialStage052);


            Button potencyButton12 = new Button(465, 126);
            potencyButton12.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton12.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage053 = TutorialFactory.getTutorial(textures["bg_tut053"], potencyButton12);
            stages.Add(tutorialStage053);

            potencyButton12.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton12.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage054 = TutorialFactory.getTutorial(textures["bg_tut054"], potencyButton12);
            stages.Add(tutorialStage054);

            potencyButton12.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton12.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage055 = TutorialFactory.getTutorial(textures["bg_tut055"], potencyButton12);
            stages.Add(tutorialStage055);

            potencyButton12.setSprite(new Sprite(textures["spr_potencyButton1"], 64, 64, 2, 4));
            potencyButton12.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage056 = TutorialFactory.getTutorial(textures["bg_tut056"], potencyButton12);
            stages.Add(tutorialStage056);

            turnButton.setSprite(new Sprite(textures["spr_turnButton"], 96, 32, 1, 3));
            turnButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.END_TURN)));
            Stage tutorialStage057 = TutorialFactory.getTutorial(textures["bg_tut057"], turnButton);
            stages.Add(tutorialStage057);

            Button upgradeButton5 = new Button(622, 150);
            upgradeButton5.setSprite(new Sprite(textures["spr_upgradeButton5"], 23, 21, 1, 1));
            upgradeButton5.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage058 = TutorialFactory.getTutorial(textures["bg_tut058"], upgradeButton5);
            stages.Add(tutorialStage058);

            Button outbreakButton2 = new Button(633, 86);
            outbreakButton2.setSprite(new Sprite(textures["spr_outbreakButton"], 64, 64, 2, 4));
            outbreakButton2.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0590 = TutorialFactory.getTutorial(textures["bg_tut059.0"], outbreakButton2);
            stages.Add(tutorialStage0590);

            Button speedButton3 = new Button(441, 86);
            speedButton3.setSprite(new Sprite(textures["spr_speedButton"], 64, 64, 2, 4));
            speedButton3.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0591 = TutorialFactory.getTutorial(textures["bg_tut059.1"], speedButton3);
            stages.Add(tutorialStage0591);

            Button resilienceButton = new Button(697, 86);
            resilienceButton.setSprite(new Sprite(textures["spr_resilienceButton"], 64, 64, 2, 4));
            resilienceButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage060 = TutorialFactory.getTutorial(textures["bg_tut060"], resilienceButton);
            stages.Add(tutorialStage060);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_CHANGE)));
            Stage tutorialStage061 = TutorialFactory.getTutorial(textures["bg_tut061"], continueButton);
            stages.Add(tutorialStage061);

            Button upgradeButton50 = new Button(540, 230);
            upgradeButton50.setSprite(new Sprite(textures["spr_upgradeButton5"], 23, 21, 1, 1));
            upgradeButton50.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage062 = TutorialFactory.getTutorial(textures["bg_tut062"], upgradeButton50);
            stages.Add(tutorialStage062);

            Button outbreakButton3 = new Button(553, 166);
            outbreakButton3.setSprite(new Sprite(textures["spr_outbreakButton"], 64, 64, 2, 4));
            outbreakButton3.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage063 = TutorialFactory.getTutorial(textures["bg_tut063"], outbreakButton3);
            stages.Add(tutorialStage063);

            turnButton.setSprite(new Sprite(textures["spr_turnButton"], 96, 32, 1, 3));
            turnButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.END_TURN)));
            Stage tutorialStage064 = TutorialFactory.getTutorial(textures["bg_tut064"], turnButton);
            stages.Add(tutorialStage064);

            Button upgradeButton6 = new Button(642, 131);
            upgradeButton6.setSprite(new Sprite(textures["spr_upgradeButton6"], 19, 18, 1, 1));
            upgradeButton6.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage065 = TutorialFactory.getTutorial(textures["bg_tut065"], upgradeButton6);
            stages.Add(tutorialStage065);

            Button outbreakButton4 = new Button(653, 66);
            outbreakButton4.setSprite(new Sprite(textures["spr_outbreakButton"], 64, 64, 2, 4));
            outbreakButton4.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage066 = TutorialFactory.getTutorial(textures["bg_tut066"], outbreakButton4);
            stages.Add(tutorialStage066);

            Button speedButton4 = new Button(461, 66);
            speedButton4.setSprite(new Sprite(textures["spr_speedButton"], 64, 64, 2, 4));
            speedButton4.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage067 = TutorialFactory.getTutorial(textures["bg_tut067"], speedButton4);
            stages.Add(tutorialStage067);

            turnButton.setSprite(new Sprite(textures["spr_turnButton"], 96, 32, 1, 3));
            turnButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.END_TURN)));
            Stage tutorialStage068 = TutorialFactory.getTutorial(textures["bg_tut068"], turnButton);
            stages.Add(tutorialStage068);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage069 = TutorialFactory.getTutorial(textures["bg_tut069"], continueButton);
            stages.Add(tutorialStage069);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage0701 = TutorialFactory.getTutorial(textures["bg_tut070.1"], continueButton);
            stages.Add(tutorialStage0701);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage0702 = TutorialFactory.getTutorial(textures["bg_tut070.2"], continueButton);
            stages.Add(tutorialStage0702);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage071 = TutorialFactory.getTutorial(textures["bg_tut071"], continueButton);
            stages.Add(tutorialStage071);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage0711 = TutorialFactory.getTutorial(textures["bg_tut071.1"], continueButton);
            stages.Add(tutorialStage0711);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage0712 = TutorialFactory.getTutorial(textures["bg_tut071.2"], continueButton);
            stages.Add(tutorialStage0712);

            Button upgradeButton60 = new Button(663, 111);
            upgradeButton60.setSprite(new Sprite(textures["spr_upgradeButton6"], 19, 18, 1, 1));
            upgradeButton60.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage072 = TutorialFactory.getTutorial(textures["bg_tut072"], upgradeButton60);
            stages.Add(tutorialStage072);

            Button potencyButton3 = new Button(609, 46);
            potencyButton3.setSprite(new Sprite(textures["spr_potencyButton3"], 64, 64, 3, 2));
            potencyButton3.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage073 = TutorialFactory.getTutorial(textures["bg_tut073"], potencyButton3);
            stages.Add(tutorialStage073);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage074 = TutorialFactory.getTutorial(textures["bg_tut074"], continueButton);
            stages.Add(tutorialStage074);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage0750 = TutorialFactory.getTutorial(textures["bg_tut075.0"], continueButton);
            stages.Add(tutorialStage0750);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage0751 = TutorialFactory.getTutorial(textures["bg_tut075.1"], continueButton);
            stages.Add(tutorialStage0751);

            continueButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            continueButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            Stage tutorialStage0752 = TutorialFactory.getTutorial(textures["bg_tut075.2"], continueButton);
            stages.Add(tutorialStage0752);

            Button upgradeButton51 = new Button(360, 150);
            upgradeButton51.setSprite(new Sprite(textures["spr_upgradeButton5"], 23, 21, 2, 1));
            upgradeButton51.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0753 = TutorialFactory.getTutorial(textures["bg_tut075.3"], upgradeButton51);
            stages.Add(tutorialStage0753);

            Button outbreakButton5 = new Button(371, 86);
            outbreakButton5.setSprite(new Sprite(textures["spr_outbreakButton"], 64, 64, 3, 2));
            outbreakButton5.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0754 = TutorialFactory.getTutorial(textures["bg_tut075.4"], outbreakButton5);
            stages.Add(tutorialStage0754);

            Button speedButton5 = new Button(181, 86);
            speedButton5.setSprite(new Sprite(textures["spr_speedButton"], 64, 64, 3, 2));
            speedButton5.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0755 = TutorialFactory.getTutorial(textures["bg_tut075.5"], speedButton5);
            stages.Add(tutorialStage0755);

            speedButton5.setSprite(new Sprite(textures["spr_speedButton"], 64, 64, 3, 2));
            speedButton5.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.TILE_CLICK)));
            Stage tutorialStage0756 = TutorialFactory.getTutorial(textures["bg_tut075.6"], speedButton5);
            stages.Add(tutorialStage0756);

            turnButton.setSprite(new Sprite(textures["spr_turnButton"], 96, 32, 1, 3));
            turnButton.setMousePressedAction(
                new NextStageGameAction(this, soundManager.getSound((int)SOUNDS.END_TURN)));
            Stage tutorialStage076 = TutorialFactory.getTutorial(textures["bg_tut076"], turnButton);
            stages.Add(tutorialStage076);

            Button endButton = new Button(825,200);
            endButton.setSprite(new Sprite(textures["spr_continueButton"], 100, 40, 2, 1));
            endButton.setMousePressedAction(new MenuGameAction(this, (int)STAGES.MAIN_MENU, soundManager.getSound((int)SOUNDS.END_TURN)));
            Stage tutorialStageEnd = TutorialFactory.getTutorial(textures["bg_tutEnd"], endButton);
            stages.Add(tutorialStageEnd);
        }

        private void buildOptionsMenu()
        {
            /*****
             *  THIS IS HOW YOU BUILD THE MENU STAGES
             * 
             * */
            MenuStage gameOptionsMenu = 
                new MenuStage("Game Options", fonts["menuFont"], GraphicsDevice.Viewport.Width / 2, 0, 
                            soundManager.getSound((int) SOUNDS.MENU_CHANGE));

            gameOptionsMenu.setBackground(textures["bg_matrix"]);
            x = GraphicsDevice.Viewport.Width / 2;

            #region Number of Turns
            MenuOption numOfTurns = new MenuOption(spriteBatch, x - 100, y + 50, "Number of turns", fonts["menuFont"], true);

            MenuChoice fifteenTurns = new MenuChoice(spriteBatch, x + 50, y + 50, "15", fonts["menuFont"], true);
            MenuChoice thirtyTurns = new MenuChoice(spriteBatch, x + 100, y + 50, "30", fonts["menuFont"], true);
            MenuChoice fortyFiveTurns = new MenuChoice(spriteBatch, x + 150, y + 50, "45", fonts["menuFont"], true);

            fifteenTurns.setEnterAction(new SetNumberofTurns(gameSettings, 15));
            thirtyTurns.setEnterAction(new SetNumberofTurns(gameSettings, 30));
            fortyFiveTurns.setEnterAction(new SetNumberofTurns(gameSettings, 45));

            gameOptionsMenu.addOption(numOfTurns);

            gameOptionsMenu.GetOptionsItem(0).addChoice(fifteenTurns);
            gameOptionsMenu.GetOptionsItem(0).addChoice(thirtyTurns);
            gameOptionsMenu.GetOptionsItem(0).addChoice(fortyFiveTurns);

            #endregion

            #region Potency

            MenuOption potency = new MenuOption(spriteBatch, x - 100, y + 100, "Potency", fonts["menuFont"], true);

            MenuChoice hundredPotency = new MenuChoice(spriteBatch, x + 50, y + 100, "50", fonts["menuFont"], true);
            MenuChoice hundredFiftyPotency = new MenuChoice(spriteBatch, x + 100, y + 100, "150", fonts["menuFont"], true);
            MenuChoice twoHundredPotency = new MenuChoice(spriteBatch, x + 150, y + 100, "200", fonts["menuFont"], true);

            hundredPotency.setEnterAction(new SetPotencyAction(gameSettings, 100));
            hundredFiftyPotency.setEnterAction(new SetPotencyAction(gameSettings, 150));
            twoHundredPotency.setEnterAction(new SetPotencyAction(gameSettings, 200));

            gameOptionsMenu.addOption(potency);

            gameOptionsMenu.GetOptionsItem(1).addChoice(hundredPotency);
            gameOptionsMenu.GetOptionsItem(1).addChoice(hundredFiftyPotency);
            gameOptionsMenu.GetOptionsItem(1).addChoice(twoHundredPotency);


            #endregion

            #region Fog Of War
            MenuOption fogOfWarOption = new MenuOption(spriteBatch, x - 100, y + 150, "Fog of War", fonts["menuFont"], true);

            MenuChoice on = new MenuChoice(spriteBatch, x + 50, y + 150, "On", fonts["menuFont"], true);
            MenuChoice off = new MenuChoice(spriteBatch, x + 100, y + 150, "Off", fonts["menuFont"], true);

            on.setEnterAction(new SetFogofWarAction(gameSettings, true));
            off.setEnterAction(new SetFogofWarAction(gameSettings, false));

            gameOptionsMenu.addOption(fogOfWarOption);

            gameOptionsMenu.GetOptionsItem(2).addChoice(on);
            gameOptionsMenu.GetOptionsItem(2).addChoice(off);

            #endregion

            #region Number of Players
            int maxNumPlayers = board.getMaxNumPlayers();
            MenuOption numPlayersOption = new MenuOption(spriteBatch, x - 100, y + 200, "Number of players", fonts["menuFont"], true);
            for (int i = 2; i <= maxNumPlayers; i++)
            {
                MenuChoice numPlayers = new MenuChoice(spriteBatch, x + 50 * (i - 1), y + 200, i.ToString(), fonts["menuFont"], true);
                numPlayers.setEnterAction(new SetNumberofPlayers(gameSettings, i));
                numPlayersOption.addChoice(numPlayers);
            }
            gameOptionsMenu.addOption(numPlayersOption);
            
            #endregion

            #region Turn Time Limit
            MenuOption timeLimitOption = new MenuOption(spriteBatch, x - 100, y + 250, "Turn time limit", fonts["menuFont"], true);

            MenuChoice noTimeLimit = new MenuChoice(spriteBatch, x + 50, y + 250, "None", fonts["menuFont"], true);
            MenuChoice tenSecLimit = new MenuChoice(spriteBatch, x + 100, y + 250, "10s", fonts["menuFont"], true);
            MenuChoice thirtySecLimit = new MenuChoice(spriteBatch, x + 150, y + 250, "30s", fonts["menuFont"], true);
            MenuChoice sixtySecLimit = new MenuChoice(spriteBatch, x + 200, y + 250, "60s", fonts["menuFont"], true);

            noTimeLimit.setEnterAction(new SetTurnTimeLimitAction(gameSettings, -1));
            tenSecLimit.setEnterAction(new SetTurnTimeLimitAction(gameSettings, 10));
            thirtySecLimit.setEnterAction(new SetTurnTimeLimitAction(gameSettings, 30));
            sixtySecLimit.setEnterAction(new SetTurnTimeLimitAction(gameSettings, 60));

            gameOptionsMenu.addOption(timeLimitOption);

            gameOptionsMenu.GetOptionsItem(4).addChoice(noTimeLimit);
            gameOptionsMenu.GetOptionsItem(4).addChoice(tenSecLimit);
            gameOptionsMenu.GetOptionsItem(4).addChoice(thirtySecLimit);
            gameOptionsMenu.GetOptionsItem(4).addChoice(sixtySecLimit);
            
            #endregion

            MenuOption backOption = new MenuOption(spriteBatch, x, y + 300, "Back", fonts["menuFont"], true);
            MenuOption startOption = new MenuOption(spriteBatch, x, y + 350, "Continue", fonts["menuFont"], true);

            backOption.setEnterAction(new MenuGameAction(this, (int)STAGES.GAMETYPE, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            startOption.setEnterAction(new MenuGameAction(this, (int)STAGES.PROFILE_SELECTION, soundManager.getSound((int)SOUNDS.MENU_SELECT)));

            gameOptionsMenu.addOption(backOption);
            gameOptionsMenu.addOption(startOption);

            stages.Insert((int)STAGES.GAME_OPTIONS, gameOptionsMenu);

        }

        private void buildProfileMenu()
        {
            x = GraphicsDevice.Viewport.Width / 2;
            profileManager.initializeSelection(gameSettings.getNumPlayers());
            MenuStage profileMenu =
                new MenuStage("Delve Game", fonts["menuFont"], x, 0,
                    soundManager.getSound((int)SOUNDS.MENU_CHANGE));

            profileMenu.setBackground(textures["bg_matrix"]);

            profileChoiceOverlay = new Rectangle(-1024, 50, 150, 50 * profileManager.getNumProfiles());
            colorChoiceOverlay = new Rectangle(-1024, 50, 150, (y + 1) * profileManager.getNumColors());

            //populate list of profiles
            for (int k = 0; k < profileManager.getNumProfiles(); k++)
            {
                playerProfileChoices.Add(new MenuChoice2(spriteBatch, x + 75, (GraphicsDevice.Viewport.Height / 2) + (50 * (k + 1)), profileManager.getProfileName(k), fonts["menuFont"], true));
                playerProfileChoices.ElementAt(k).setEnterAction(new CloseProfileChoicesGameAction(this, 0, k));
            }

            //populate list of colors
            for (int k = 0; k < profileManager.getNumColors(); k++)
            {
                colorChoices.Add(new MenuChoice2(spriteBatch, x + 75, (GraphicsDevice.Viewport.Height / 2) + (50 * (k + 1)), profileManager.getColor(k).ToString(), fonts["menuFont"], true));
                colorChoices.ElementAt(k).setEnterAction(new CloseColorChoicesGameAction(this, 0, k));
            }


            //Populate List of players and colors for the game by column
            for (int i = 0; i < gameSettings.getNumPlayers(); i++)
            {
                MenuOption playerOption = new MenuOption(spriteBatch, x - 75, 50 * (2*i + 1), "Player " + (i + 1), fonts["menuFont"], true);
                playerOption.setEnterAction(new OpenProfileChoicesGameAction(this, (2*i)));
                profileMenu.addOption(playerOption);
                menuOptions.Add(playerOption);

                MenuOption colorOption = new MenuOption(spriteBatch, x - 75, 50 * (2*i + 2), "Color " + (i + 1), fonts["menuFont"], true);
                colorOption.setEnterAction(new OpenColorChoicesGameAction(this, (2*i + 1)));
                profileMenu.addOption(colorOption);
                menuOptions.Add(colorOption);
            }

            for (int i = 0; i < gameSettings.getNumPlayers(); i++)
            {
                //add profile choices for each option
                for (int j = 0; j < profileManager.getNumProfiles(); j++)
                {
                    menuOptions[2 * i].addChoice2(playerProfileChoices.ElementAt(j));
                }

                //add color choices for each option
                for (int k = 0; k < profileManager.getNumColors(); k++)
                {
                    menuOptions[((2 * i) + 1)].addChoice2(colorChoices.ElementAt(k));
                }
            }

            MenuOption createProfileOption = new MenuOption(spriteBatch, x, y + 300, "Create New Profile", fonts["menuFont"], true);
            createProfileOption.setEnterAction(new MenuGameAction(this, (int)STAGES.PROFILE_CREATION, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            profileMenu.addOption(createProfileOption);

            MenuOption backOption = new MenuOption(spriteBatch, x, y + 350, "Back", fonts["menuFont"], true);
            backOption.setEnterAction(new MenuGameAction(this, (int)STAGES.GAME_OPTIONS, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            profileMenu.addOption(backOption);

            MenuOption startOption = new MenuOption(spriteBatch, x, y + 400, "Start", fonts["menuFont"], true);
            startOption.setEnterAction(new MenuBeginGameAction(this));
            profileMenu.addOption(startOption);
            stages.Insert((int)STAGES.PROFILE_SELECTION, profileMenu);
        }

        private void buildCreateProfile()
        {
            //call update for keyboard, on update, -> MenuOption.setName = string -> once enter pressed->call enter action to profileManager.createProfile
            x = GraphicsDevice.Viewport.Width / 2;
            
            MenuStage profileCreationMenu =
                new MenuStage("Enter Name for profile (20 Chars Max).", fonts["menuFont"], x, 0,
                    soundManager.getSound((int)SOUNDS.MENU_CHANGE));

            profileCreationMenu.setBackground(textures["bg_matrix"]);

            profileName = new MenuOption(spriteBatch, x, y + 100, "_", fonts["menuFont"], true);
            profileName.setEnterAction(new CreateProfileGameAction(this));
            profileCreationMenu.addOption(profileName);

            MenuOption backOption = new MenuOption(spriteBatch, x, y + 150, "Back", fonts["menuFont"], true);
            backOption.setEnterAction(new MenuGameAction(this, (int)STAGES.PROFILE_SELECTION, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            profileCreationMenu.addOption(backOption);

            stages.Insert((int)STAGES.PROFILE_CREATION, profileCreationMenu);
        }

        private void buildMainMenu()
        {
            gameSettings = new GameSettings();
            MenuStage mainMenu = 
                new MenuStage("Delve Game", fonts["menuFont"], GraphicsDevice.Viewport.Width / 2, 0,
                    soundManager.getSound((int)SOUNDS.MENU_CHANGE));

            x = GraphicsDevice.Viewport.Width / 2;
            y = 0;

            //background
            mainMenu.setBackground(textures["bg_matrix"]);

            MenuOption playGameOption = new MenuOption(spriteBatch, x, y + 50, "Play Game", fonts["menuFont"],true);
            MenuOption createMapsOption = new MenuOption(spriteBatch, x, y + 100, "Create Map", fonts["menuFont"], true);
            MenuOption viewTutorialOption = new MenuOption(spriteBatch, x, y + 150, "View Tutorial", fonts["menuFont"], true);
            MenuOption viewLeaderboardOption = new MenuOption(spriteBatch, x, y + 200, "View Leaderboards", fonts["menuFont"], true);
            MenuOption quitGameOption = new MenuOption(spriteBatch, x, y + 250, "Quit Game", fonts["menuFont"], true);
            
            playGameOption.setEnterAction(
                new MenuGameAction(this, (int)STAGES.GAME_CONNECTION, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            createMapsOption.setEnterAction(
                new MenuGameAction(this, (int)STAGES.MAP_CREATION_MENU, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            viewTutorialOption.setEnterAction(
                new MenuGameAction(this, (int)STAGES.TUTORIAL_START, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            viewLeaderboardOption.setEnterAction(
                new MenuGameAction(this, (int)STAGES.LEADERBOARD_START, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            quitGameOption.setEnterAction(
                new MenuGameAction(this, (int)STAGES.END_GAME, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
          

            mainMenu.addOption(playGameOption);
            mainMenu.addOption(createMapsOption);
            mainMenu.addOption(viewTutorialOption);
            mainMenu.addOption(viewLeaderboardOption);
            mainMenu.addOption(quitGameOption);

            stages.Add(mainMenu);
        }

        private void buildGametypeMenu()
        {
            MenuStage gameTypeMenu = 
                new MenuStage("Select Gametype", fonts["menuFont"], GraphicsDevice.Viewport.Width / 2, 0,
                    soundManager.getSound((int)SOUNDS.MENU_CHANGE));
            profileManager.readFromFile();
            board = mapManager.getBoard();

            gameTypeMenu.setBackground(textures["bg_matrix"]);
            int yOffset = 50;
            MenuOption enduranceOption = new MenuOption(spriteBatch, x, y + yOffset, "Endurance", fonts["menuFont"], true);
            enduranceOption.setEnterAction(
                new SetGametypeAction(gameSettings, (int)GameType.ENDURANCE, this,
                    soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            gameTypeMenu.addOption(enduranceOption);
            yOffset += 50;
            if (board.hasTerritories())
            {
                MenuOption territoryOption = new MenuOption(spriteBatch, x, y + 100, "Territories", fonts["menuFont"], true);
                territoryOption.setEnterAction(
                    new SetGametypeAction(gameSettings, (int)GameType.TERRITORIES, this,
                        soundManager.getSound((int)SOUNDS.MENU_SELECT)));
                gameTypeMenu.addOption(territoryOption);
                yOffset += 50;
            }
            if (board.hasRaceTerritory())
            {
                MenuOption raceTerritoryOption = new MenuOption(spriteBatch, x, y + yOffset, "Race to a Territory", fonts["menuFont"], true);
                raceTerritoryOption.setEnterAction(
                    new SetGametypeAction(gameSettings, (int)GameType.TERRITORY_RACE, this,
                        soundManager.getSound((int)SOUNDS.MENU_SELECT)));
                gameTypeMenu.addOption(raceTerritoryOption);
                yOffset += 50;
            }
            MenuOption quickOption = new MenuOption(spriteBatch, x, y + yOffset, "Quick Game", fonts["menuFont"], true);
            quickOption.setEnterAction(
                new SetGametypeAction(gameSettings, (int)GameType.QUICK_WIN, this,
                    soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            gameTypeMenu.addOption(quickOption);
            yOffset += 50;

            MenuOption backOption = new MenuOption(spriteBatch, x, y + yOffset, "Back", fonts["menuFont"], true);
            backOption.setEnterAction(
                new MenuGameAction(this, 0, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            gameTypeMenu.addOption(backOption);
            stages.Insert((int)STAGES.GAMETYPE,gameTypeMenu);
        }
        
        private void buildLeaderboardMenu()
        {
            Actor bgBox = new Actor();
            bgBox.setLocation(100, 100);
            bgBox.setSprite(new Sprite(textures["spr_plain"], 400, 400, 1, 1));
            bgBox.setColor(Color.Black);

            Stage leaderboardEndurance = new Stage();
            leaderboardEndurance.setBackground(textures["bg_matrix"]);
            leaderboardEndurance.add(bgBox);
            TextComponent componentTitleEndur = new TextComponent(fonts["menuFont"], 150, 120);
            componentTitleEndur.setString("      ENDURANCE \nLEADERS       WINS");
            leaderboardEndurance.add(componentTitleEndur);
            int enduranceLeaderboardLocation = stages.Count;
            stages.Add(leaderboardEndurance);

            Stage leaderboardTerritories = new Stage();
            leaderboardTerritories.setBackground(textures["bg_matrix"]);
            leaderboardTerritories.add(bgBox);
            TextComponent componentTitleTerr = new TextComponent(fonts["menuFont"], 150, 120);
            componentTitleTerr.setString("      TERRITORIES \nLEADERS       WINS");
            leaderboardTerritories.add(componentTitleTerr);
            int territoriesLeaderboardLocation = stages.Count;
            stages.Add(leaderboardTerritories);


            Stage leaderboardQuickWin = new Stage();
            leaderboardQuickWin.setBackground(textures["bg_matrix"]);
            leaderboardQuickWin.add(bgBox);
            TextComponent componentTitleQW = new TextComponent(fonts["menuFont"], 150, 120);
            componentTitleQW.setString("      QUICK WIN \nLEADERS       WINS");
            leaderboardQuickWin.add(componentTitleQW);
            int quickWinLeaderboardLocation = stages.Count;
            stages.Add(leaderboardQuickWin);


            Stage leaderboardTerrRace = new Stage();
            leaderboardTerrRace.setBackground(textures["bg_matrix"]);
            leaderboardTerrRace.add(bgBox);
            TextComponent componentTitleTR = new TextComponent(fonts["menuFont"], 150, 120);
            componentTitleTR.setString("      TERRITORY RACE \nLEADERS       WINS");
            leaderboardTerrRace.add(componentTitleTR);
            int terrRaceLeaderboardLocation = stages.Count;
            stages.Add(leaderboardTerrRace);

            profileManager.readFromFile();
            List<PlayerProfile> list = Leaderboard.getList("endurance", profileManager.playerProfiles);

            int i = 0;
            while(i < list.Count){
                TextComponent component = new TextComponent(fonts["menuFont"], 150, 170 + 20 * i);
                component.setString((i+1) + ". " + list[i].name + "      " + list[i].wins[0]);
                leaderboardEndurance.add(component);
                i++;
            }

            list = Leaderboard.getList("territories", profileManager.playerProfiles);
            i = 0;
            while(i < list.Count){
                TextComponent component = new TextComponent(fonts["menuFont"], 150, 170 + 20 * i);
                component.setString((i+1) + ". " + list[i].name + "      " + list[i].wins[1]);
                leaderboardTerritories.add(component);
                i++;
            }

            list = Leaderboard.getList("quickwin", profileManager.playerProfiles);
            i = 0;
            while(i < list.Count){
                TextComponent component = new TextComponent(fonts["menuFont"], 150, 170 + 20 * i);
                component.setString((i+1) + ". " + list[i].name + "      " + list[i].wins[2]);
                leaderboardQuickWin.add(component);
                i++;
            }

            list = Leaderboard.getList("terrRace", profileManager.playerProfiles);
            i = 0;
            while(i < list.Count){
                TextComponent component = new TextComponent(fonts["menuFont"], 150, 170 + 20 * i);
                component.setString((i+1) + ". " + list[i].name + "      " + list[i].wins[3]);
                leaderboardTerrRace.add(component);
                i++;
            }

            Button toEnduranceLeaderboard = new Button(50, 50);
            toEnduranceLeaderboard.setSprite(new Sprite(textures["spr_enduranceButton"], 129, 34, 3, 1));
            ChangeStageGameAction ga = new ChangeStageGameAction(this, enduranceLeaderboardLocation,
                soundManager.getSound((int)SOUNDS.MENU_SELECT));
            toEnduranceLeaderboard.setMouseReleasedAction(ga);

            Button toQuickWinLeaderboard = new Button(200, 50);
            toQuickWinLeaderboard.setSprite(new Sprite(textures["spr_quickWinButton"], 129, 34, 3, 1));
            ga = new ChangeStageGameAction(this, quickWinLeaderboardLocation,
                soundManager.getSound((int)SOUNDS.MENU_SELECT));
            toQuickWinLeaderboard.setMouseReleasedAction(ga);

            Button toTerritoriesLeaderboard = new Button(350, 50);
            toTerritoriesLeaderboard.setSprite(new Sprite(textures["spr_territoriesButton"], 129, 34, 3, 1));
            ga = new ChangeStageGameAction(this, territoriesLeaderboardLocation,
                soundManager.getSound((int)SOUNDS.MENU_SELECT));
            toTerritoriesLeaderboard.setMouseReleasedAction(ga);

            Button toTerrRaceLeaderboard = new Button(500, 50);
            toTerrRaceLeaderboard.setSprite(new Sprite(textures["spr_terrRaceButton"], 129, 34, 3, 1));
            ga = new ChangeStageGameAction(this, terrRaceLeaderboardLocation,
                soundManager.getSound((int)SOUNDS.MENU_SELECT));
            toTerrRaceLeaderboard.setMouseReleasedAction(ga);

            Button back = new Button(250, 510);
            back.setSprite(new Sprite(textures["spr_backButton"], 129, 34, 3, 1));
            ga = new ChangeStageGameAction(this, 0,
                soundManager.getSound((int)SOUNDS.MENU_SELECT));
            back.setMouseReleasedAction(ga);

            leaderboardEndurance.add(toEnduranceLeaderboard);
            leaderboardEndurance.add(toTerritoriesLeaderboard);
            leaderboardEndurance.add(toQuickWinLeaderboard);
            leaderboardEndurance.add(toTerrRaceLeaderboard);
            leaderboardEndurance.add(back);

            leaderboardTerritories.add(toEnduranceLeaderboard);
            leaderboardTerritories.add(toTerritoriesLeaderboard);
            leaderboardTerritories.add(toQuickWinLeaderboard);
            leaderboardTerritories.add(toTerrRaceLeaderboard);
            leaderboardTerritories.add(back);

            leaderboardQuickWin.add(toEnduranceLeaderboard);
            leaderboardQuickWin.add(toTerritoriesLeaderboard);
            leaderboardQuickWin.add(toQuickWinLeaderboard);
            leaderboardQuickWin.add(toTerrRaceLeaderboard);
            leaderboardQuickWin.add(back);

            leaderboardTerrRace.add(toEnduranceLeaderboard);
            leaderboardTerrRace.add(toTerritoriesLeaderboard);
            leaderboardTerrRace.add(toQuickWinLeaderboard);
            leaderboardTerrRace.add(toTerrRaceLeaderboard);
            leaderboardTerrRace.add(back);

        }
    
        private void buildReplayMenu()
        {
            x = GraphicsDevice.Viewport.Width / 2;
            
            MenuStage replayMenu = 
                new MenuStage("Replays", fonts["menuFont"], x, 0,
                soundManager.getSound((int)SOUNDS.MENU_CHANGE));
            //populate MenuItems objects for each replay available, extract from replay tags
            replayManager.readFromFile();
            List<MenuOption> replayOptions = new List<MenuOption>();

            for (int i = 0; i < replayManager.numReplays(); i++)
            {
                replayOptions.Add(new MenuOption(spriteBatch, x, y + (50 * (i+1)),
                     Path.GetFileNameWithoutExtension(replayManager.loggedGames.ElementAt(i).fileName), fonts["menuFont"], true));
                replayOptions.ElementAt(i).setEnterAction(new MenuBeginReplayGameAction(this, 5, i));
                replayMenu.addOption(replayOptions.ElementAt(i));
            }
            replayMenu.setBackground(textures["bg_matrix"]);

            MenuOption backOption = new MenuOption(spriteBatch, x, (y + 50 * replayManager.numReplays() + 50), "Back", fonts["menuFont"], true);
            backOption.setEnterAction(new MenuGameAction(this, (int)STAGES.MAIN_MENU, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            replayMenu.addOption(backOption);

            //add to stages if hasn't been built, else rebuild and add (to look at new replays)
            stages.Insert((int)STAGES.REPLAY_MENU,replayMenu);
        }

        private void buildMapCreationMenu()
        {
            x = GraphicsDevice.Viewport.Width / 2;
            mapManager.readFromFile();
            MenuStage mapMenu = new MenuStage("Maps", fonts["menuFont"], x, 0,
                soundManager.getSound((int)SOUNDS.MENU_CHANGE));
            //populate MenuItems objects for each map available, extract name from map tags
            List<MenuOption> mapOptions = new List<MenuOption>();
            int i;
            for (i = 0; i < mapManager.getNumMaps(); i++)
            {
                mapOptions.Add(new MenuOption(spriteBatch, x, y + (50 * (i + 1)), mapManager.maps.ElementAt(i).fileName, fonts["menuFont"], true));
                mapOptions.ElementAt(i).setEnterAction(new EditMapSelectGameAction(this, mapManager, i));
                mapMenu.addOption(mapOptions.ElementAt(i));
            }
            mapMenu.setBackground(textures["bg_matrix"]);

            mapOptions.Add(new MenuOption(spriteBatch, x, y + (50 * (i + 1)), "Create New Map", fonts["menuFont"], true));
            mapOptions.ElementAt(i).setEnterAction(new EditMapSelectGameAction(this, mapManager, i));
            mapMenu.addOption(mapOptions.ElementAt(i));
            i++;

            MenuOption backOption = new MenuOption(spriteBatch, x, (y + 50 * (i+1)), "Back", fonts["menuFont"], true);
            backOption.setEnterAction(new MenuGameAction(this, (int)STAGES.MAIN_MENU, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            mapMenu.addOption(backOption);


            //MenuChoice editChoice = new MenuChoice(spriteBatch, x - 100, y + 1000, "Edit", fonts["menuFont"]);
            //MenuChoice confirmChoice = new MenuChoice(spriteBatch, x + 100, y + 1000, "Confirm", fonts["menuFont"]);

            mapMenu.setBackground(textures["bg_matrix"]);

            stages.Insert((int)STAGES.MAP_CREATION_MENU, mapMenu);
        }

        private void buildMapSelectionMenu()
        {
            x = GraphicsDevice.Viewport.Width / 2;
            mapManager.readFromFile();
            MenuStage mapMenu = new MenuStage("Maps", fonts["menuFont"], x, 0,
                soundManager.getSound((int)SOUNDS.MENU_CHANGE));
            //populate MenuItems objects for each map available, extract name from map tags
            List<MenuOption> mapOptions = new List<MenuOption>();
            int i;
            for (i = 0; i < mapManager.getNumMaps(); i++)
            {
                mapOptions.Add(new MenuOption(spriteBatch, x, y + (50 * (i + 1)), mapManager.maps.ElementAt(i).fileName, fonts["menuFont"], true));
                mapOptions.ElementAt(i).setEnterAction(new PlayMapSelectGameAction(this, mapManager, i));
                mapMenu.addOption(mapOptions.ElementAt(i));
            }
            mapMenu.setBackground(textures["bg_matrix"]);

            MenuOption backOption = new MenuOption(spriteBatch, x, (y + 50 * (i + 1)), "Back", fonts["menuFont"], true);
            backOption.setEnterAction(new MenuGameAction(this, (int)STAGES.MAIN_MENU, soundManager.getSound((int)SOUNDS.MENU_SELECT)));
            mapMenu.addOption(backOption);


            //MenuChoice editChoice = new MenuChoice(spriteBatch, x - 100, y + 1000, "Edit", fonts["menuFont"]);
            //MenuChoice confirmChoice = new MenuChoice(spriteBatch, x + 100, y + 1000, "Confirm", fonts["menuFont"]);

            mapMenu.setBackground(textures["bg_matrix"]);

            stages.Insert((int)STAGES.MAP_SELECTION, mapMenu);
        }

        public override void Update(GameTime gameTime)
        {
            if (game != null)
            {
                if (currentStage == playStageIndex)
                {
                    bool changeTurn = game.Update(gameTime);
                    if (changeTurn)
                    {
                        gotoNextStage();
                        game.changeTurn();
                        SoundEffectInstance sound = soundManager.getSound((int)SOUNDS.END_TURN);
                        sound.Play();
                    }
                }
                if (game.isGameOver())
                {
                    int winnerIndex = game.getWinner().getPlayerIndex();
                    gameOverMessage.setString("And the winner is " + profileManager.getPlayerName(winnerIndex) + "!");
                    currentStage = (int)STAGES.GAME_OVER;
                    replayManager.logGame(game.getGameLog());
                    for(int i = 0; i < game.gameSettings.numPlayers; i++) {
                        profileManager.SavePlayer(game.getPlayer(i), gameSettings.getGameType());
                    }
                    profileManager.writeToFile();
                    game = null;
                    sound.Stop();
                    sound = soundManager.getSound((int)SOUNDS.MENU_BGM);
                    sound.IsLooped = true;
                    sound.Play();
                } 
            }
            
            else if (currentStage == (int)STAGES.PROFILE_CREATION)
            {
                textEditor.Update();
                if (profileName.getMenuName().Length <= 21)
                    profileName.setMenuName(textEditor.getInputText() + '_');
            }

            else if (currentStage == (int)STAGES.ENTER_IP_ADDRESS)
            {
                textEditor.Update();
                if (ipAddress.getMenuName().Length <= 21)
                    ipAddress.setMenuName(textEditor.getInputText() + '_');
            }
            stages[currentStage].update();
            //updateAllianceButtons();
            if (gameSettings.gameType == (int)GameType.TERRITORIES)
            {
                updatePlayerScores();
            }
            doMouseActions();
            base.Update(gameTime);
            //oldKeysPressed = new List<Keys>();
        }

        /*private void updateAllianceButtons()
        {
            List<int> potentialAllies = game.canAllyWith();
            switch (game.getCurrentPlayerIndex())
            {
                case 0:
                    if(game.getCurrentPlayer().isAlly(game.getPlayer(1)))
                    {
                        breakAllianceButton01.visible = true;
                        acceptRequestButton01.visible = false;
                        sendRequestButton01.visible = false;
                    }
                    else if (requestSent10)
                    {
                        acceptRequestButton01.visible = true;
                    }
                    else if (potentialAllies.Contains(1))
                    {
                        if (!this.requestSent01)
                        {
                            sendRequestButton01.visible = true;
                        }
                        else
                        {
                            sendRequestButton01.visible = false;
                            cancelRequestButton01.visible = true;
                        }
                    }

                    if(game.getCurrentPlayer().isAlly(game.getPlayer(2)))
                    {
                        breakAllianceButton02.visible = true;
                        acceptRequestButton02.visible = false;
                        sendRequestButton02.visible = false;
                    }
                    else if (requestSent20)
                    {
                        acceptRequestButton02.visible = true;
                    }
                    else if (potentialAllies.Contains(2))
                    {
                        if (!this.requestSent02)
                        {
                            sendRequestButton02.visible = true;
                        }
                        else
                        {
                            sendRequestButton02.visible = false;
                            cancelRequestButton02.visible = true;
                        }
                    }

                    if (game.getCurrentPlayer().isAlly(game.getPlayer(3)))
                    {
                        breakAllianceButton03.visible = true;
                        acceptRequestButton03.visible = false;
                        sendRequestButton03.visible = false;
                    }
                    else if (requestSent30)
                    {
                        acceptRequestButton03.visible = true;
                    }
                    else if (potentialAllies.Contains(3))
                    {
                        if (!this.requestSent03)
                        {
                            sendRequestButton03.visible = true;
                        }
                        else
                        {
                            sendRequestButton03.visible = false;
                            cancelRequestButton03.visible = true;
                        }
                    }

                    sendRequestButton10.visible = false;
                    sendRequestButton12.visible = false;
                    sendRequestButton13.visible = false;

                    sendRequestButton20.visible = false;
                    sendRequestButton21.visible = false;
                    sendRequestButton23.visible = false;

                    sendRequestButton30.visible = false;
                    sendRequestButton31.visible = false;
                    sendRequestButton32.visible = false;

                    cancelRequestButton10.visible = false;
                    cancelRequestButton12.visible = false;
                    cancelRequestButton13.visible = false;

                    cancelRequestButton20.visible = false;
                    cancelRequestButton21.visible = false;
                    cancelRequestButton23.visible = false;

                    cancelRequestButton03.visible = false;
                    cancelRequestButton31.visible = false;
                    cancelRequestButton32.visible = false;

                    acceptRequestButton10.visible = false;
                    acceptRequestButton12.visible = false;
                    acceptRequestButton13.visible = false;

                    acceptRequestButton20.visible = false;
                    acceptRequestButton21.visible = false;
                    acceptRequestButton23.visible = false;

                    acceptRequestButton30.visible = false;
                    acceptRequestButton31.visible = false;
                    acceptRequestButton32.visible = false;
                    break;

                case 1:

                    sendRequestButton01.visible = false;
                    sendRequestButton02.visible = false;
                    sendRequestButton03.visible = false;

                    if (game.getCurrentPlayer().isAlly(game.getPlayer(0)))
                    {
                        breakAllianceButton10.visible = true;
                        acceptRequestButton10.visible = false;
                        sendRequestButton10.visible = false;
                    }
                    else if (requestSent01)
                    {
                        acceptRequestButton10.visible = true;
                    }
                    else if (potentialAllies.Contains(0))
                    {
                        if (!this.requestSent10)
                        {
                            sendRequestButton10.visible = true;
                        }
                        else
                        {
                            sendRequestButton10.visible = false;
                            cancelRequestButton10.visible = true;
                        }
                    }

                    if (game.getCurrentPlayer().isAlly(game.getPlayer(2)))
                    {
                        breakAllianceButton12.visible = true;
                        acceptRequestButton12.visible = false;
                        sendRequestButton12.visible = false;
                    }
                    else if (requestSent21)
                    {
                        acceptRequestButton12.visible = true;
                    }
                    else if (potentialAllies.Contains(2))
                    {
                        if (!this.requestSent12)
                        {
                            sendRequestButton12.visible = true;
                        }
                        else
                        {
                            sendRequestButton12.visible = false;
                            cancelRequestButton12.visible = true;
                        }
                    }

                    if (game.getCurrentPlayer().isAlly(game.getPlayer(3)))
                    {
                        breakAllianceButton13.visible = true;
                        acceptRequestButton13.visible = false;
                        sendRequestButton13.visible = false;
                    }
                    else if (requestSent31)
                    {
                        acceptRequestButton13.visible = true;
                    }
                    else if (potentialAllies.Contains(3))
                    {
                        if (!this.requestSent13)
                        {
                            sendRequestButton13.visible = true;
                        }
                        else
                        {
                            sendRequestButton13.visible = false;
                            cancelRequestButton13.visible = true;
                        }
                    }

                    sendRequestButton20.visible = false;
                    sendRequestButton21.visible = false;
                    sendRequestButton23.visible = false;

                    sendRequestButton30.visible = false;
                    sendRequestButton31.visible = false;
                    sendRequestButton32.visible = false;

                    cancelRequestButton01.visible = false;
                    cancelRequestButton02.visible = false;
                    cancelRequestButton03.visible = false;

                    cancelRequestButton20.visible = false;
                    cancelRequestButton21.visible = false;
                    cancelRequestButton23.visible = false;

                    cancelRequestButton30.visible = false;
                    cancelRequestButton31.visible = false;
                    cancelRequestButton32.visible = false;

                    acceptRequestButton01.visible = false;
                    acceptRequestButton02.visible = false;
                    acceptRequestButton03.visible = false;

                    acceptRequestButton20.visible = false;
                    acceptRequestButton21.visible = false;
                    acceptRequestButton23.visible = false;

                    acceptRequestButton30.visible = false;
                    acceptRequestButton31.visible = false;
                    acceptRequestButton32.visible = false;
                    break;

                case 2:
                    sendRequestButton01.visible = false;
                    sendRequestButton02.visible = false;
                    sendRequestButton03.visible = false;

                    sendRequestButton10.visible = false;
                    sendRequestButton12.visible = false;
                    sendRequestButton13.visible = false;



                    if (game.getCurrentPlayer().isAlly(game.getPlayer(0)))
                    {
                        breakAllianceButton20.visible = true;
                        acceptRequestButton20.visible = false;
                        sendRequestButton20.visible = false;
                    }
                    else if (requestSent02)
                    {
                        acceptRequestButton20.visible = true;
                    }
                    else if (potentialAllies.Contains(0))
                    {
                        if (!this.requestSent20)
                        {
                            sendRequestButton20.visible = true;
                        }
                        else
                        {
                            sendRequestButton20.visible = false;
                            cancelRequestButton20.visible = true;
                        }
                    }

                    if (game.getCurrentPlayer().isAlly(game.getPlayer(1)))
                    {
                        breakAllianceButton21.visible = true;
                        acceptRequestButton21.visible = false;
                        sendRequestButton21.visible = false;
                    }
                    else if (requestSent12)
                    {
                        acceptRequestButton21.visible = true;
                    }
                    else
                        if (potentialAllies.Contains(1))
                        {
                            if (!this.requestSent21)
                            {
                                sendRequestButton21.visible = true;
                            }
                            else
                            {
                                sendRequestButton21.visible = false;
                                cancelRequestButton21.visible = true;
                            }
                        }


                    if (game.getCurrentPlayer().isAlly(game.getPlayer(3)))
                    {
                        breakAllianceButton23.visible = true;
                        acceptRequestButton23.visible = false;
                        sendRequestButton23.visible = false;
                    }
                    else if (requestSent32)
                    {
                        acceptRequestButton23.visible = true;
                    }
                    else if (potentialAllies.Contains(3))
                    {
                        if (!this.requestSent23)
                        {
                            sendRequestButton23.visible = true;
                        }
                        else
                        {
                            sendRequestButton23.visible = false;
                            cancelRequestButton23.visible = true;
                        }
                    }

                    sendRequestButton30.visible = false;
                    sendRequestButton31.visible = false;
                    sendRequestButton32.visible = false;

                    cancelRequestButton01.visible = false;
                    cancelRequestButton02.visible = false;
                    cancelRequestButton03.visible = false;

                    cancelRequestButton10.visible = false;
                    cancelRequestButton12.visible = false;
                    cancelRequestButton13.visible = false;

                    cancelRequestButton30.visible = false;
                    cancelRequestButton31.visible = false;
                    cancelRequestButton32.visible = false;

                    acceptRequestButton01.visible = false;
                    acceptRequestButton02.visible = false;
                    acceptRequestButton03.visible = false;

                    acceptRequestButton10.visible = false;
                    acceptRequestButton12.visible = false;
                    acceptRequestButton13.visible = false;

                    acceptRequestButton30.visible = false;
                    acceptRequestButton31.visible = false;
                    acceptRequestButton32.visible = false;

                    break;

                case 3:
                    sendRequestButton01.visible = false;
                    sendRequestButton02.visible = false;
                    sendRequestButton03.visible = false;

                    sendRequestButton10.visible = false;
                    sendRequestButton12.visible = false;
                    sendRequestButton13.visible = false;

                    sendRequestButton20.visible = false;
                    sendRequestButton21.visible = false;
                    sendRequestButton23.visible = false;



                    if (game.getCurrentPlayer().isAlly(game.getPlayer(0)))
                    {
                        breakAllianceButton30.visible = true;
                        acceptRequestButton30.visible = false;
                        sendRequestButton30.visible = false;
                    }
                    else if (requestSent03)
                    {
                        acceptRequestButton30.visible = true;
                    }
                    else if (potentialAllies.Contains(0))
                    {
                        if (!this.requestSent30)
                        {
                            sendRequestButton30.visible = true;
                        }
                        else
                        {
                            sendRequestButton30.visible = false;
                            cancelRequestButton30.visible = true;
                        }
                    }

                    if (game.getCurrentPlayer().isAlly(game.getPlayer(1)))
                    {
                        breakAllianceButton31.visible = true;
                        acceptRequestButton31.visible = false;
                        sendRequestButton31.visible = false;
                    }
                    else if (requestSent13)
                    {
                        acceptRequestButton31.visible = true;
                    }
                    else if (potentialAllies.Contains(1))
                    {
                        if (!this.requestSent31)
                        {
                            sendRequestButton31.visible = true;
                        }
                        else
                        {
                            sendRequestButton31.visible = false;
                            cancelRequestButton31.visible = true;
                        }
                        sendRequestButton31.visible = true;
                    }

                    if (game.getCurrentPlayer().isAlly(game.getPlayer(2)))
                    {
                        breakAllianceButton32.visible = true;
                        acceptRequestButton32.visible = false;
                        sendRequestButton32.visible = false;
                    }
                    else if (requestSent23)
                    {
                        acceptRequestButton32.visible = true;
                    }
                    else if (potentialAllies.Contains(2))
                    {
                        if (!this.requestSent32)
                        {
                            sendRequestButton32.visible = true;
                        }
                        else
                        {
                            sendRequestButton32.visible = false;
                            cancelRequestButton32.visible = true;
                        }
                    }

                    cancelRequestButton01.visible = false;
                    cancelRequestButton02.visible = false;
                    cancelRequestButton03.visible = false;

                    cancelRequestButton10.visible = false;
                    cancelRequestButton12.visible = false;
                    cancelRequestButton13.visible = false;

                    cancelRequestButton20.visible = false;
                    cancelRequestButton21.visible = false;
                    cancelRequestButton23.visible = false;

                    acceptRequestButton01.visible = false;
                    acceptRequestButton02.visible = false;
                    acceptRequestButton03.visible = false;

                    acceptRequestButton10.visible = false;
                    acceptRequestButton12.visible = false;
                    acceptRequestButton13.visible = false;

                    acceptRequestButton20.visible = false;
                    acceptRequestButton21.visible = false;
                    acceptRequestButton23.visible = false;
                    break;
            }
        }/**/

        #region Mouse Methods
        private void doMouseActions()
        {
            MouseState ms = Mouse.GetState();

            if (ms.LeftButton == ButtonState.Pressed)
            {
                if (isMouseLeftReleased)
                {
                    stages[currentStage].mousePressed(ms.X, ms.Y);
                    isMouseLeftReleased = false;
                    
                }
            }
            else if (ms.LeftButton == ButtonState.Released)
            {
                if (!isMouseLeftReleased)
                {
                    //Console.WriteLine("Released");
                    stages[currentStage].mouseReleased(ms.X, ms.Y);
                    isMouseLeftReleased = true;
                }
                else
                {
                    stages[currentStage].mouseOver(ms.X, ms.Y);
                }
            }  
        }
        #endregion

        public override void Draw(GameTime gameTime)
        {
            //draw overlays here
            if (currentStage == (int)STAGES.PROFILE_SELECTION)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(textures["drag_rectangle"], profileChoiceOverlay, Color.White);
                spriteBatch.Draw(textures["drag_rectangle"], colorChoiceOverlay, Color.White);
                spriteBatch.End();
            }

            stages[currentStage].draw(spriteBatch);
            base.Draw(gameTime);
        }

        internal void gotoNextStage()
        {
            currentStage++;
        }

        internal void gotoPreviousStage()
        {
            currentStage--;
        }

        internal void gotoStage(int index)
        {
            currentStage = index;
        }

        internal static void setHudText(String str)
        {
            hudString = str;
        }

        internal static String getHudText()
        {
            return hudString;
        }

        internal static void setHudTileType(int type)
        {
            hudTileType = type;
        }

        internal static int getHudTileType()
        {
            return hudTileType;
        }

        internal void setBackgroundMusic(SoundEffectInstance newSound)
        {
            sound = newSound;
            sound.IsLooped = true;
            sound.Play();
        }

        private void updatePlayerScores()
        {
            if (game != null)
            {
                String str = "";
                for (int i = 0; i < gameSettings.getNumPlayers(); i++)
                {
                    str += profileManager.getPlayerName(i) + ": " + game.getPlayer(i).getPoints() + "\n";
                }
                playerScores.setString(str);
            }
        }

        /*internal void setRequestSent(int targetPlayer)
        {
            switch (game.getCurrentPlayerIndex())
            {
                case 0:
                    switch (targetPlayer)
                    {
                        case 1:
                            this.requestSent01 = true;
                            break;
                        case 2:
                            this.requestSent02 = true;
                            break;
                        case 3:
                            this.requestSent03 = true;
                            break;
                    }
                    break;

                case 1:
                    switch (targetPlayer)
                    {
                        case 0:
                            this.requestSent10 = true;
                            break;
                        case 2:
                            this.requestSent12 = true;
                            break;
                        case 3:
                            this.requestSent13 = true;
                            break;
                    }
                    break;

                case 2:
                    switch (targetPlayer)
                    {
                        case 0:
                            this.requestSent20 = true;
                            break;
                        case 1:
                            this.requestSent21 = true;
                            break;
                        case 3:
                            this.requestSent23 = true;
                            break;
                    }
                    break;

                case 3:
                    switch (targetPlayer)
                    {
                        case 0:
                            this.requestSent30 = true;
                            break;
                        case 1:
                            this.requestSent31 = true;
                            break;
                        case 2:
                            this.requestSent32 = true;
                            break;
                    }
                    break;
            }
        }/**/

        /*
        internal void setRequestAccepted(int sendingPlayer)
        {
            switch (game.getCurrentPlayerIndex())
            {
                case 0:
                    switch (sendingPlayer)
                    {
                        case 1:
                            this.requestAccepted10 = true;
                            break;
                        case 2:
                            this.requestAccepted20 = true;
                            break;
                        case 3:
                            this.requestAccepted30 = true;
                            break;
                    }
                    break;

                case 1:
                    switch (sendingPlayer)
                    {
                        case 0:
                            this.requestAccepted01 = true;
                            break;
                        case 2:
                            this.requestAccepted21 = true;
                            break;
                        case 3:
                            this.requestAccepted31 = true;
                            break;
                    }
                    break;

                case 2:
                    switch (sendingPlayer)
                    {
                        case 0:
                            this.requestAccepted02 = true;
                            break;
                        case 1:
                            this.requestAccepted12 = true;
                            break;
                        case 3:
                            this.requestAccepted32 = true;
                            break;
                    }
                    break;

                case 3:
                    switch (sendingPlayer)
                    {
                        case 0:
                            this.requestAccepted03 = true;
                            break;
                        case 1:
                            this.requestAccepted13 = true;
                            break;
                        case 2:
                            this.requestAccepted23 = true;
                            break;
                    }
                    break;
            }
        }/**/

        public void keyPressed( Keys key )
        {
            stages[currentStage].keyPressed(key);
        }

        internal void quitGame()
        {
            replayManager.writeToFile();
            Game.Exit();

        }

        /*internal void restartGame()
        {
            game.resetGame(gameSettings);
        }
         * */


        internal void applyUpgrade(Tile tile, int upgrade)
        {
            game.applyUpgrade(tile, upgrade);
        }

        internal void applyUpgrades(List<Tile> list, int upgrade)
        {
            game.applyUpgrades(list, upgrade);
        }

        internal List<int> doMove(List<Tile> tiles)
        {
            return game.doMove(tiles);
        }

        internal Player getCurrentPlayer()
        {
            return game.getCurrentPlayer();
        }

        internal int getCurrentPlayerIndex()
        {
            return game.getCurrentPlayerIndex();
        }

        internal void changeTurn()
        {
            game.changeTurn();
        }

        internal void buildReplayStage()
        {
            //construct new game with gamelog
            //GameLog gamelog = replayManager.getReplay();

            //build stage to run game log
        }

        internal void beginGame()
        {

            try
            {
                buildPlayStage();
                sound.Stop();
                sound = soundManager.getSound((int)SOUNDS.GAME_BGM);
                sound.IsLooped = true;
                sound.Play();
                gotoStage(playStageIndex);
            }
            catch (TimeoutException e) {
                ipAddress.setMenuName("_");
            }
        }

        internal void setNetwork(bool networked)
        {
            isNetworked = networked;
        }

        internal bool getNetwork()
        {
            return isNetworked;
        }

        internal void setHost(bool host)
        {
            isHost = host;
        }

        internal bool getIsHost()
        {
            return isHost;
        }

        internal void submitIPAddress()
        {
            string menuName = ipAddress.menuName;
            if (menuName.Length > 0)
            {
                ipAddress.setMenuName(menuName.Remove(menuName.Length - 1));
            }
        }

        #region resetMenus



        internal void resetReplayMenu()
        {
            stages.RemoveAt((int)STAGES.REPLAY_MENU);
            buildReplayMenu();
        }

        internal void resetGametypeMenu()
        {
            stages.RemoveAt((int)STAGES.GAMETYPE);
            buildGametypeMenu();
        }

        internal void resetMapCreationMenu()
        {
            stages.RemoveAt((int)STAGES.MAP_CREATION_MENU);
            buildMapCreationMenu();
        }

        internal void resetMapSelectionMenu()
        {
            stages.RemoveAt((int)STAGES.MAP_SELECTION);
            buildMapSelectionMenu();
        }

        internal void resetMapCreator()
        {
            stages.RemoveAt((int)STAGES.MAP_CREATOR + 1);
            stages.RemoveAt((int)STAGES.MAP_CREATOR);
            buildMapCreatorStages();
        }

        internal void resetOptionsMenu()
        {
            stages.RemoveAt((int)STAGES.GAME_OPTIONS);
            buildOptionsMenu();
        }

        #endregion

        internal void setReplayToLoad(int replayIndex)
        {
            replayManager.selectReplay(replayIndex);
        }

        internal void expandProfileChoices()
        {
            //creates rectangle based on available profiles
            //add list of choices available to "drop down menu". Expand if player is selected, after profile is selected, set player with selected profile, remove rectangle
            profileChoiceOverlay.X = (GraphicsDevice.Viewport.Width / 2) + 75;

            for (int i = 0; i < playerProfileChoices.Count; i++)
            {
                foreach (MenuOption op in menuOptions)
                {
                    playerProfileChoices.ElementAt(i).setVisibility(true);       
                }

                playerProfileChoices.ElementAt(i).setPosition((GraphicsDevice.Viewport.Width / 2) + 75, (i + 1) * 50);
            }
        }

        internal void expandColorChoices()
        {
            //creates rectangle based on available profiles
            //add list of choices available to "drop down menu". Expand if player is selected, after color is selected, set player with selected profile, move rectangle into position
            colorChoiceOverlay.X = (1024 / 2) + 75;

            for (int i = 0; i < colorChoices.Count - 1; i++)
            {
                foreach (MenuOption op in menuOptions)
                {
                    if (op.menuName != colorChoices.ElementAt(i).menuName)
                    {
                        colorChoices.ElementAt(i).setVisibility(true);
                    }
                }

                colorChoices.ElementAt(i).setPosition(-1024, (i + 1) * 50);
            }
        }

        internal void selectProfile(int playerIndex, int profileIndex)
        {
            //move rectangle drop down choices off screen, display profile as menu option by replacing menuOption name with profile name, set choice visibility = false
            profileManager.selectProfile(playerIndex, profileIndex);
            profileChoiceOverlay.X = -1024;

            //for each profile choice, move them out of sight
            for(int i = 0; i < playerProfileChoices.Count - 1; i++)
            {
                foreach (MenuOption op in menuOptions)
                {
                    if (op.menuName != playerProfileChoices.ElementAt(i).menuName)
                    {
                        playerProfileChoices.ElementAt(i).setVisibility(true);
                    }
                }

                playerProfileChoices.ElementAt(i).setPosition(-1024, (i + 1) * 50); 
            }

            //set name of option to profile selected
            menuOptions[profileMenuOptionIndex].setMenuName(playerProfileChoices.ElementAt(profileIndex).menuName);
            playerProfileChoices.ElementAt(profileIndex).setVisibility(false);

        }

        internal void selectColor(int playerIndex, int colorIndex)
        {
            //move rectangle drop down choices off screen, display profile as menu option by replacing menuOption name with color name, set choice visibility = false
            //colorIndex = color element selected in color choices

            profileManager.selectColor(playerIndex, colorIndex);
            colorChoiceOverlay.X = -1024;

            //for each color choice, move them out of sight
            for (int i = 0; i < colorChoices.Count - 1; i++)
            {
                colorChoices.ElementAt(i).setPosition(-1024, (i + 1) * 50);
            }
            
            //set name of option to color selected
            menuOptions[profileMenuOptionIndex].setMenuName(colorChoices.ElementAt(colorIndex).menuName);
            colorChoices.ElementAt(colorIndex).setVisibility(false);
        }

        internal void setProfileOptionIndex(int optionIndex)
        {
            profileMenuOptionIndex = optionIndex;
        }

        internal void createProfile()
        {
            string menuName = profileName.menuName;
            if (menuName.Length > 0)
            {
                profileName.setMenuName(menuName.Remove(menuName.Length - 1));
            }

            profileManager.createProfile(menuName);
            profileName.setMenuName("_");
            rebuildProfiles();
            gotoStage((int)STAGES.PROFILE_SELECTION);
        }

        internal void rebuildProfiles()
        {
            //clean out old menu list
            stages.RemoveAt((int)STAGES.PROFILE_SELECTION);
            //re-add repopulated list of replays
            buildProfileMenu();
        }
    }
}